阿里云ossandroid端
public class GetAndUploadFileDemo {
private static String TAG = "GetAndUploadFileDemo";
private OSSService ossService;
private OSSBucket bucket;
public void show() {
ossService = OSSServiceProvider.getService();
bucket = ossService.getOssBucket("youcaidao");
// 文件的常规操作如普通上传、下载、拷贝、删除等,与Data类一致,故这里只给出断点下载和断点上传的demo
resumableDownloadWithSpecConfig();
// delay();
// resumableUpload();
// delay();
// resumableDownload();
// delay();
}
public void delay() {
try {
Thread.sleep(30 * 1000);
} catch (Exception e) {
e.printStackTrace();
}
}
// 断点上传
public void resumableUpload() {
// OSSData ossData = ossService.getOssData(sampleBucket, "sample-data");
// ossData.setData(data, "raw"); // 指定需要上传的数据和它的类型
// ossData.enableUploadCheckMd5sum(); // 开启上传MD5校验
// ossData.upload(); // 上传失败将会抛出异常
OSSFile bigfFile = ossService.getOssFile(bucket, "de.jpg");
try {
bigfFile.setUploadFilePath(
"/storage/emulated/0/Android/data/com.qd.videorecorder/video/VMS_1439866564822.jpg",
"image/jpg");
bigfFile.ResumableUploadInBackground(new SaveCallback() {
@Override
public void onSuccess(String objectKey) {
Log.d(TAG, "[onSuccess] - " + objectKey
+ " upload success!");
}
@Override
public void onProgress(String objectKey, int byteCount,
int totalSize) {
Log.d(TAG, "[onProgress] - current upload " + objectKey
+ " bytes: " + byteCount + " in total: "
+ totalSize);
}
@Override
public void onFailure(String objectKey,
OSSException ossException) {
Log.e(TAG, "[onFailure] - upload " + objectKey
+ " failed!\n" + ossException.toString());
ossException.printStackTrace();
ossException.getException().printStackTrace();
}
});
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// 断点下载
public void resumableDownload() {
OSSFile bigFile = ossService.getOssFile(bucket, "bigFile.dat");
bigFile.ResumableDownloadToInBackground(
"/storage/sdcard0/src_file/bigFile.dat", new GetFileCallback() {
@Override
public void onSuccess(String objectKey, String filePath) {
Log.d(TAG, "[onSuccess] - " + objectKey
+ " storage path: " + filePath);
}
@Override
public void onProgress(String objectKey, int byteCount,
int totalSize) {
Log.d(TAG, "[onProgress] - current download: "
+ objectKey + " bytes:" + byteCount
+ " in total:" + totalSize);
}
@Override
public void onFailure(String objectKey,
OSSException ossException) {
Log.e(TAG, "[onFailure] - download " + objectKey
+ " failed!\n" + ossException.toString());
ossException.printStackTrace();
}
});
}
// 设置相关参数的断点续传
public void resumableDownloadWithSpecConfig() {
OSSFile bigFile = ossService
.getOssFile(bucket, "VMS_1439866564822.jpg");
ResumableTaskOption option = new ResumableTaskOption();
option.setAutoRetryTime(2); // 默认为2次,最大3次
option.setThreadNum(2); // 默认并发3个线程,最大5个
bigFile.ResumableDownloadToInBackground(
"/storage/emulated/0/Android/data/com.qd.videorecorder/video/VMS_1439866564822.jpg",
new GetFileCallback() {
// /storage/emulated/0/DCIM/Camera/VID_20150803_173350.mp4
@Override
public void onSuccess(String objectKey, String filePath) {
System.out.println("[onSuccess] - " + objectKey
+ " storage path: " + filePath);
}
@Override
public void onProgress(String objectKey, int byteCount,
int totalSize) {
System.out.println("[onProgress] - current download: "
+ objectKey + " bytes:" + byteCount
+ " in total:" + totalSize);
}
@Override
public void onFailure(String objectKey,
OSSException ossException) {
System.out.println("[onFailure] - download "
+ objectKey + " failed!\n"
+ ossException.toString());
ossException.printStackTrace();
}
});
}
}