如需使用視頻點播為音視頻等媒體文件進行媒體處理(轉碼、審核、添加水印等)、分發播放等操作,則需要先將媒體文件上傳到視頻點播中。本文以Java SDK調用API為例介紹如何調用API上傳媒體文件,并提供完整的API上傳Demo供參考。
背景信息
通過視頻點播API上傳,實際是基于OSS原生SDK上傳,需要開發者自行實現所有上傳邏輯,包括在點播服務獲取上傳地址和憑證、Base64解碼上傳憑證和地址、以及調用OSS能力完成上傳。此方式較為繁瑣且出錯率較高,因此,本文封裝完整API上傳Demo供開發者參考,以避免您在通過視頻點播API上傳時,出現媒資一直處于上傳中的問題。
點播API上傳是基于OSS的上傳,更多上傳原理請參見OSS上傳概述。
本文以Java語言為例提供說明,其他語言可以參考代碼邏輯自行實現,或參考基于OSS原生SDK上傳。
更多咨詢及建議,請加入服務釘釘群(群號:11370001915)獲取技術支持。
前提條件
您已經開通了視頻點播服務。開通步驟請參見開通視頻點播。
您已授權視頻點播服務VOD訪問OSS,可訪問云資源訪問授權頁面進行授權。
如果您使用的是RAM用戶,則需要對該RAM用戶授予VOD和OSS的管理權限,詳細操作請參見創建RAM用戶并授權,權限策略介紹請參見系統授權策略。
步驟一:安裝點播服務端SDK
操作指引請參見點播Java SDK安裝。
步驟二:安裝OSS SDK
操作指引請參見OSS Java SDK安裝。
步驟三:上傳
以下代碼示例展示了如何調用OSS能力來實現本地上傳、網絡流/網絡URL上傳、本地M3U8上傳、分片上傳以及斷點續傳,如何在點播服務獲取上傳地址和憑證、Base64解碼上傳憑證和地址等完整的上傳示例請參見完整代碼示例。
本地上傳
public static void uploadLocalFile(OSSClient ossClient, JSONObject uploadAddress, String localFile) {
String bucketName = uploadAddress.getString("Bucket");
String objectName = uploadAddress.getString("FileName");
try {
File file = new File(localFile);
PutObjectResult result = ossClient.putObject(bucketName, objectName, file);
// 如果上傳成功,則返回200。
// 上傳文件的同時指定進度條參數。此處UploadOss為調用類的類名,請在實際使用時替換為相應的類名。
// PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName,objectName, file).
// <PutObjectRequest>withProgressListener(new UploadOss()));
// System.out.println(result.getResponse().getStatusCode());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 關閉OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
網絡流、網絡URL上傳
public static void uploadURLFile(OSSClient ossClient, JSONObject uploadAddress, String url) {
String bucketName = uploadAddress.getString("Bucket");
String objectName = uploadAddress.getString("FileName");
try {
InputStream inputStream = new URL(url).openStream();
PutObjectResult result = ossClient.putObject(bucketName, objectName, inputStream);
// 如果上傳成功,則返回200。
System.out.println(result.getResponse().getStatusCode());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 關閉OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
本地M3U8上傳
上傳本地M3U8音視頻文件(包括所有分片文件)到點播,需上傳本地M3U8索引文件地址和所有分片地址。
public static void uploadLocalM3U8(OSSClient ossClient, JSONObject uploadAddress, String indexFile, String[] tsFiles ) {
String bucketName = uploadAddress.getString("Bucket");
String objectName = uploadAddress.getString("FileName");
String objectPrefix = uploadAddress.getString("ObjectPrefix");
System.out.println(uploadAddress.toJSONString());
try {
//上傳索引文件indexFile
File index = new File(indexFile);
ossClient.putObject(bucketName, objectName, index);
Thread.sleep(200);
//上傳ts文件 tsFiles
for(String filePath : tsFiles){
File ts = new File(filePath);
ossClient.putObject(bucketName, objectPrefix + ts.getName(), ts);
Thread.sleep(200);
}
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 關閉OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
分片上傳以及斷點續傳
public static void uploadEnableCheckPointFile(OSSClient ossClient, JSONObject uploadAddress, String localFile) {
String bucketName = uploadAddress.getString("Bucket");
String objectName = uploadAddress.getString("FileName");
try {
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName,objectName);
// 通過UploadFileRequest設置單個參數。
// 填寫本地文件的完整路徑,例如D:\\localpath\\examplefile.mp4。如果未指定本地路徑,則默認從示例程序所屬項目對應本地路徑中上傳文件。
uploadFileRequest.setUploadFile(localFile);
// 指定上傳并發線程數,默認值為1。
uploadFileRequest.setTaskNum(5);
// 指定上傳的分片大小,單位為字節,取值范圍為100 KB~5 GB。默認值為100 KB。
uploadFileRequest.setPartSize(1 * 1024 * 1024);
// 開啟斷點續傳,默認關閉。
uploadFileRequest.setEnableCheckpoint(true);
// 記錄本地分片上傳結果的文件。上傳過程中的進度信息會保存在該文件中,如果某一分片上傳失敗,再次上傳時會根據文件中記錄的點繼續上傳。上傳完成后,該文件會被刪除。
// 如果未設置該值,默認與待上傳的本地文件同路徑,名稱為${uploadFile}.ucp。
//uploadFileRequest.setCheckpointFile("yourCheckpointFile");
// 斷點續傳上傳。
ossClient.uploadFile(uploadFileRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 關閉OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
完整代碼示例
import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.event.ProgressEvent;
import com.aliyun.oss.event.ProgressEventType;
import com.aliyun.oss.event.ProgressListener;
import com.aliyun.oss.model.*;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.*;
import org.apache.commons.codec.binary.Base64;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
/**
* ***** 使用須知 ******!!
* 以下Java示例代碼演示了如何通過API 方式上傳媒資文件至視頻點播。OSS部分詳細請參考 http://bestwisewords.com/document_detail/32013.html
* 此demo不同于上傳SDK,如何使用上傳SDK參考 http://bestwisewords.com/document_detail/52200.html
* <p>
* 一、普通音視頻上傳
*
* 1.上傳本地文件,使用分片上傳,并支持斷點續傳。
* 1.1 當斷點續傳關閉時,最大支持上傳任務執行時間為3000秒,具體可上傳文件大小與您的網絡帶寬及磁盤讀寫能力有關。 參見uploadEnableCheckPointFile函數。
* 1.2 當斷點續傳開啟時,最大支持48.8TB的單個文件,注意,斷點續傳開啟后,上傳任務執行過程中,同時會將當前上傳位置寫入本地磁盤文件,影響您上傳文件的速度,請您根據文件大小選擇是否開啟
*
* 2.上傳網絡流,可指定文件URL進行上傳,最大支持48.8TB的單個文件。參見uploadURLFile函數。
*
* 3.上傳文件流,可指定本地文件進行上傳,不支持斷點續傳,最大支持5GB的單個文件。參見uploadLocalFile函數。
*
* <p>
* 二、m3u8文件上傳:
* 1.上傳本地m3u8音視頻文件(包括所有分片文件)到點播,需上傳本地m3u8索引文件地址和所有分片地址。參見uploadLocalM3U8函數
* 2.上傳網絡m3u8音視頻文件建議使用URL方式。
*
* 注:
* 1) 上傳網絡m3u8音視頻文件時需要保證地址可訪問,如果有權限限制,請設置帶簽名信息的地址,且保證足夠長的有效期,防止地址無法訪問導致上傳失敗
* <p>
*
* 三、上傳進度:
* 1.默認上傳進度回調函數:必須實現ProgressListener類, 若不需要可不實現此類。參考 http://bestwisewords.com/document_detail/84796.html ;
* 2.自定義上傳進度回調函數:您可根據自已的業務場景重新定義不同事件處理的方式,只需要修改上傳回調示例函數即可。
* 3.本demo中,參見第 225 行代碼示例
*
* <p>
*
* 四、接口說明:
* 1.CreateUploadVideo 和 RefreshUploadVideo 都只是獲取憑證的接口,根據業務需要二選一即可
* 2.RefreshUploadVideo 可用于憑證超時后重新獲取,也可用于覆蓋文件上傳,videoId保持不變
* 3.CreateUploadImage 可用于獲取圖片上傳憑證
* 4.CreateUploadAttachedMedia 可用于獲取輔助媒資上傳憑證
* 3.接口詳細說明參考 http://bestwisewords.com/document_detail/436731.html
*
* <p>
*
* 注意:
* 請替換示例中的必選參數,示例中的可選參數如果您不需要設置,請將其刪除,以免設置無效參數值與您的預期不符。
*/
public class UploadVodByApiDemo implements ProgressListener {
//上傳進度條相關參數,非必須
private long bytesWritten = 0;
private long totalBytes = -1;
private boolean succeed = false;
public static void main(String[] argv) {
// 阿里云賬號AccessKey擁有所有API的訪問權限,建議您使用RAM用戶進行API訪問或日常運維。
// 強烈建議不要把AccessKey ID和AccessKey Secret保存到工程代碼里,否則可能導致AccessKey泄露,威脅您賬號下所有資源的安全。
// 本示例通過從環境變量中讀取AccessKey,來實現API訪問的身份驗證。運行代碼示例前,請配置環境變量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
//需要上傳到VOD的本地視頻文件的完整路徑,需要包含文件擴展名
String localFile = "E:/demo/demo.mp4";
// 填寫網絡流地址。可訪問的url即可
String url = "https://bucket-name*****.oss-cn-shanghai.aliyuncs.com/demo/demo.mp4";
try {
// 初始化VOD客戶端并獲取上傳地址和憑證
DefaultAcsClient vodClient = initVodClient(accessKeyId, accessKeySecret);
//獲取視頻上傳憑證
CreateUploadVideoResponse createUploadVideoResponse = createUploadVideo(vodClient);
//獲取刷新上傳憑證
//RefreshUploadVideoResponse refreshUploadVideoResponse = refreshUploadVideo(vodClient);
//獲取圖片上傳憑證
//CreateUploadImageResponse createUploadImageResponse = createUploadImage(vodClient);
//獲取輔助媒資上傳憑證
//CreateUploadAttachedMediaResponse createUploadAttachedMediaResponse = createUploadAttachedMedia(vodClient);
// 執行成功會返回VideoId、UploadAddress和UploadAuth
String videoId = createUploadVideoResponse.getVideoId();
JSONObject uploadAuth = JSONObject.parseObject(decodeBase64(createUploadVideoResponse.getUploadAuth()));
JSONObject uploadAddress = JSONObject.parseObject(decodeBase64(createUploadVideoResponse.getUploadAddress()));
//使用UploadAuth和UploadAddress初始化OSS客戶端
// 通過這個OSSClient對象,傳入uploadAuth和uploadAddress,即可完成視頻文件到阿里云OSS的上傳
OSSClient ossClient = initOssClient(uploadAuth, uploadAddress);
// 上傳方式, 按需選擇
// 1.本地上傳文件,注意是同步上傳會阻塞等待,耗時與文件大小和網絡上行帶寬有關
uploadLocalFile(ossClient, uploadAddress, localFile);
// 2.網絡文件上傳
//uploadURLFile(ossClient, uploadAddress, url);
// 3.分片、斷點續傳
//uploadEnableCheckPointFile(ossClient, uploadAddress, localFile);
// 4.上傳m3u8
//String indexFile = "E:/demo/demo.m3u8";
//String[] tsFiles = {"E:/demo/demo_01.ts"};
//uploadLocalM3U8(ossClient, uploadAddress, indexFile, tsFiles);
System.out.println("Put local file succeed, VideoId : " + videoId);
} catch (Exception e) {
System.out.println("Put local file fail, ErrorMessage : " + e.getLocalizedMessage());
}
}
/**
* 初始化VOD客戶端
* @throws ClientException
*/
public static DefaultAcsClient initVodClient(String accessKeyId, String accessKeySecret) throws ClientException {
// 點播服務接入區域,中國內地請填cn-shanghai,其他區域請參考文檔 http://bestwisewords.com/document_detail/98194.html
String regionId = "cn-shanghai";
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
DefaultAcsClient client = new DefaultAcsClient(profile);
return client;
}
/**
* 初始化OSS客戶端
* @throws ClientException
*/
public static OSSClient initOssClient(JSONObject uploadAuth, JSONObject uploadAddress) {
String endpoint = uploadAddress.getString("Endpoint");
String accessKeyId = uploadAuth.getString("AccessKeyId");
String accessKeySecret = uploadAuth.getString("AccessKeySecret");
String securityToken = uploadAuth.getString("SecurityToken");
return new OSSClient(endpoint, accessKeyId, accessKeySecret, securityToken);
}
/**
* 獲取上傳憑證
* @throws ClientException
*/
public static CreateUploadVideoResponse createUploadVideo(DefaultAcsClient vodClient) throws ClientException {
CreateUploadVideoRequest request = new CreateUploadVideoRequest();
request.setFileName("vod_test.m3u8");
request.setTitle("this is title");
//request.setDescription("this is desc");
//request.setTags("tag1,tag2");
//request.setCoverURL("http://vod.****.com/test_cover_url.jpg");
//request.setCateId(-1L);
//request.setTemplateGroupId("34f055******c7af499c73");
//request.setWorkflowId("");
//request.setStorageLocation("");
//request.setAppId("app-1000000");
return vodClient.getAcsResponse(request);
}
/**
* 獲取刷新憑證
* @throws ClientException
*/
public static RefreshUploadVideoResponse refreshUploadVideo(DefaultAcsClient vodClient) throws ClientException {
RefreshUploadVideoRequest request = new RefreshUploadVideoRequest();
request.setAcceptFormat(FormatType.JSON);
request.setVideoId("VideoId");
//設置請求超時時間
request.setSysReadTimeout(1000);
request.setSysConnectTimeout(1000);
return vodClient.getAcsResponse(request);
}
/**
* 獲取圖片上傳憑證
* @throws ClientException
*/
public static CreateUploadImageResponse createUploadImage(DefaultAcsClient vodClient) throws ClientException {
CreateUploadImageRequest request = new CreateUploadImageRequest();
request.setImageType("default");
request.setAcceptFormat(FormatType.JSON);
//設置請求超時時間
request.setSysReadTimeout(1000);
request.setSysConnectTimeout(1000);
return vodClient.getAcsResponse(request);
}
/**
* 獲取輔助媒資上傳憑證
* @throws ClientException
*/
public static CreateUploadAttachedMediaResponse createUploadAttachedMedia(DefaultAcsClient vodClient) throws ClientException {
CreateUploadAttachedMediaRequest request = new CreateUploadAttachedMediaRequest();
request.setBusinessType("watermark");
request.setMediaExt("png");
request.setAcceptFormat(FormatType.JSON);
//設置請求超時時間
request.setSysReadTimeout(1000);
request.setSysConnectTimeout(1000);
return vodClient.getAcsResponse(request);
}
/**
* 本地文件上傳
* @throws Exception
*/
public static void uploadLocalFile(OSSClient ossClient, JSONObject uploadAddress, String localFile) {
String bucketName = uploadAddress.getString("Bucket");
String objectName = uploadAddress.getString("FileName");
try {
File file = new File(localFile);
PutObjectResult result = ossClient.putObject(bucketName, objectName, file);
// 如果上傳成功,則返回200。
// 上傳文件的同時指定進度條參數。此處UploadOss為調用類的類名,請在實際使用時替換為相應的類名。
// PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName,objectName, file).
// <PutObjectRequest>withProgressListener(new UploadOss()));
// System.out.println(result.getResponse().getStatusCode());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 關閉OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 網絡流式上傳
* @throws Exception
*/
public static void uploadURLFile(OSSClient ossClient, JSONObject uploadAddress, String url) {
String bucketName = uploadAddress.getString("Bucket");
String objectName = uploadAddress.getString("FileName");
try {
InputStream inputStream = new URL(url).openStream();
PutObjectResult result = ossClient.putObject(bucketName, objectName, inputStream);
// 如果上傳成功,則返回200。
System.out.println(result.getResponse().getStatusCode());
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 關閉OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 斷點續傳上傳
* @throws Exception
*/
public static void uploadEnableCheckPointFile(OSSClient ossClient, JSONObject uploadAddress, String localFile) {
String bucketName = uploadAddress.getString("Bucket");
String objectName = uploadAddress.getString("FileName");
try {
UploadFileRequest uploadFileRequest = new UploadFileRequest(bucketName,objectName);
// 通過UploadFileRequest設置單個參數。
// 填寫本地文件的完整路徑,例如D:\\localpath\\examplefile.mp4。如果未指定本地路徑,則默認從示例程序所屬項目對應本地路徑中上傳文件。
uploadFileRequest.setUploadFile(localFile);
// 指定上傳并發線程數,默認值為1。
uploadFileRequest.setTaskNum(5);
// 指定上傳的分片大小,單位為字節,取值范圍為100 KB~5 GB。默認值為100 KB。
uploadFileRequest.setPartSize(1 * 1024 * 1024);
// 開啟斷點續傳,默認關閉。
uploadFileRequest.setEnableCheckpoint(true);
// 記錄本地分片上傳結果的文件。上傳過程中的進度信息會保存在該文件中,如果某一分片上傳失敗,再次上傳時會根據文件中記錄的點繼續上傳。上傳完成后,該文件會被刪除。
// 如果未設置該值,默認與待上傳的本地文件同路徑,名稱為${uploadFile}.ucp。
//uploadFileRequest.setCheckpointFile("yourCheckpointFile");
// 斷點續傳上傳。
ossClient.uploadFile(uploadFileRequest);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 關閉OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
/**
* 本地上傳m3u8
* @throws Exception
*/
public static void uploadLocalM3U8(OSSClient ossClient, JSONObject uploadAddress, String indexFile, String[] tsFiles ) {
String bucketName = uploadAddress.getString("Bucket");
String objectName = uploadAddress.getString("FileName");
String objectPrefix = uploadAddress.getString("ObjectPrefix");
System.out.println(uploadAddress.toJSONString());
try {
//上傳索引文件indexFile
File index = new File(indexFile);
ossClient.putObject(bucketName, objectName, index);
Thread.sleep(200);
//上傳ts文件 tsFiles
for(String filePath : tsFiles){
File ts = new File(filePath);
ossClient.putObject(bucketName, objectPrefix + ts.getName(), ts);
Thread.sleep(200);
}
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (Throwable ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
// 關閉OSSClient。
if (ossClient != null) {
ossClient.shutdown();
}
}
}
// 使用進度條需要您重寫回調方法,以下僅為參考示例。
@Override
public void progressChanged(ProgressEvent progressEvent) {
long bytes = progressEvent.getBytes();
ProgressEventType eventType = progressEvent.getEventType();
switch (eventType) {
case TRANSFER_STARTED_EVENT:
System.out.println("Start to upload......");
break;
case REQUEST_CONTENT_LENGTH_EVENT:
this.totalBytes = bytes;
System.out.println(this.totalBytes + " bytes in total will be uploaded to OSS");
break;
case REQUEST_BYTE_TRANSFER_EVENT:
this.bytesWritten += bytes;
if (this.totalBytes != -1) {
int percent = (int)(this.bytesWritten * 100.0 / this.totalBytes);
System.out.println(bytes + " bytes have been written at this time, upload progress: " + percent + "%(" + this.bytesWritten + "/" + this.totalBytes + ")");
} else {
System.out.println(bytes + " bytes have been written at this time, upload ratio: unknown" + "(" + this.bytesWritten + "/...)");
}
break;
case TRANSFER_COMPLETED_EVENT:
this.succeed = true;
System.out.println("Succeed to upload, " + this.bytesWritten + " bytes have been transferred in total");
break;
case TRANSFER_FAILED_EVENT:
System.out.println("Failed to upload, " + this.bytesWritten + " bytes have been transferred");
break;
default:
break;
}
}
public static String decodeBase64(String s) {
byte[] b = null;
String result = null;
if (s != null) {
Base64 decoder = new Base64();
try {
b = decoder.decode(s);
result = new String(b, "utf-8");
} catch (Exception e) {
}
}
return result;
}
}