雖然官方文檔說明的比較詳細了,我還是決定自己記錄一下,可以較好的梳理條理,加深理解
轉載請註明出處
[我的博客]http://www.lostbug.cn
- 初始化
一般情況下直接使用默認設置,不用單獨配置。 可以配置超時時長、分片上傳閥值等。
Configuration config = new Configuration.Builder()
.chunkSize(256 * 1024) //分片上傳時,每片的大小。 默認 256K
.putThreshhold(512 * 1024) // 啓用分片上傳閥值。默認 512K
.connectTimeout(10) // 鏈接超時。默認 10秒
.responseTimeout(60) // 服務器響應超時。默認 60秒
.recorder(recorder) // recorder 分片上傳時,已上傳片記錄器。默認 null
.recorder(recorder, keyGen) // keyGen 分片上傳時,生成標識符,用於片記錄器區分是那個文件的上傳記錄
.zone(Zone.zone0) // 設置區域,指定不同區域的上傳域名、備用域名、備用IP。默認 Zone.zone0
.build();
// 重用 uploadManager。一般地,只需要創建一個 uploadManager 對象
UploadManager uploadManager = new UploadManager(config);
- 上傳文件
七牛上傳文件主要用到兩個方法:
一、 UploadManager.put(data, key, token, handler, options);
/**
* UploadManager.put參數說明:
*
*@data byte[]/String/File 數據,可以是byte數組,文件路徑,文件。
*@key String 保存在服務器上的資源唯一標識。請參考鍵值對。
*@token String 服務器分配的token。
*@completionHandler UpCompletionHandler 上傳回調函數,必填。
*@options UploadOptions 如果需要進度通知、crc校驗、中途取消、指定mimeType則需要填寫相應字段,詳見UploadOptions參數說明。
*/
二、 UploadOptions(params, mimeType, checkCrc, progressHandler, cancellationSignal);
/**
* UploadOptions參數說明:
*
*參數 類型 說明
*params Map<String, String> 自定義變量,key必須以 x: 開始。
*mimeType String 指定文件的mimeType。
*checkCrc boolean 是否驗證上傳文件。
*progressHandler UpProgressHandler 上傳進度回調。
*cancellationSignal UpCancellationSignal 取消上傳,當isCancelled()返回true時,不再執行更多上傳。
*/
現在就比較清楚了
下面主要是各種上傳姿勢了
- 簡單上傳
// 簡單上傳
// 重用 uploadManager。一般地,只需要創建一個 uploadManager 對象
UploadManager uploadManager = new UploadManager();
data = <File對象、或 文件路徑、或 字節數組>
String key = <指定七牛服務上的文件名,或 null>;
String token = <從服務端SDK獲取>;
uploadManager.put(data, key, token,
/**
* UpCompletionHandler參數說明
*
*@key 即uploadManager.put(file, key, ...)方法指定的key。
*@info http請求的狀態信息等,可記入日誌。isOK()返回 true表示上傳成功。
*@response 七牛反饋的信息。可從中解析保存在七牛服務的key等信息,具體字段取決於上傳策略的設置。
*/
new UpCompletionHandler() {
@Override
public void complete(String key, ResponseInfo info, JSONObject res) {
// res 包含hash、key等信息,具體字段取決於上傳策略的設置。
Log.i("qiniu", key + ",\r\n " + info + ",\r\n " + res);
}
}, null);
- 記錄上傳進度
//記錄上傳進度
private void uploadProgress() {
uploadManager.put(data, key, token, handler,
new UploadOptions(null, null, false,
new UpProgressHandler() {
/**
* @param key 即uploadManager.put(file, key, ...)方法指定的key
* @param percent 進度
*/
public void progress(String key, double percent) {
Log.i("qiniu", key + ": " + percent);
}
}, null));
}
- 取消上傳
內部代碼會檢測UpCancellationSignal##isCancelled()的返回值,當其返回true時,將停止上傳。 可外部維護一個變量isCancelled,當點擊取消按鈕時,設置isCancelled = true;。如:
// 初始化、執行上傳
private volatile boolean isCancelled = false;
uploadManager.put(data, key, token,handler,
new UploadOptions(null, null, false, progressHandler,
new UpCancellationSignal(){
public boolean isCancelled(){
return isCancelled;
}
}));
// 點擊取消按鈕,讓 UpCancellationSignal##isCancelled() 方法返回 true ,以停止上傳
private void cancell() {
isCancelled = true;
}
- 記錄斷點
分片上傳中,可將各個已上傳的塊記錄下來,再次上傳時,已上傳的部分不用再次上傳。 斷點記錄類需實現 com.qiniu.android.storage.Recorder 接口。已提供保存到文件的 FileRecorder 實現。
String dirPath = <斷點記錄文件保存的文件夾位置>
Recorder recorder = new FileRecorder(dirPath);
//默認使用 key 的url_safe_base64編碼字符串作爲斷點記錄文件的文件名。
//避免記錄文件衝突(特別是key指定爲null時),也可自定義文件名(下方爲默認實現):
KeyGenerator keyGen = new KeyGenerator(){
public String gen(String key, File file){
// 不必使用url_safe_base64轉換,uploadManager內部會處理
// 該返回值可替換爲基於key、文件內容、上下文的其它信息生成的文件名
return key + "_._" + new StringBuffer(file.getAbsolutePath()).reverse();
}
};
// 重用 uploadManager。一般地,只需要創建一個 uploadManager 對象
//UploadManager uploadManager = new UploadManager(recorder); // 1
//UploadManager uploadManager = new UploadManager(recorder, keyGen); // 2
// 或 在初始化時指定:
Configuration config = new Configuration.Builder()
// recorder 分片上傳時,已上傳片記錄器
// keyGen 分片上傳時,生成標識符,用於片記錄器區分是那個文件的上傳記錄
.recorder(recorder, keyGen)
.build();
UploadManager uploadManager = new UploadManager(config);
uploadManager.put(data, key, ...)