开发者社区> 问答> 正文

Android-SDK之如何实现上传文件(二)?


Append Object 追加上传




Append Object以追加写的方式上传文件。通过Append Object操作创建的Object类型为Appendable Object,而通过Put Object上传的Object是Normal Object。

  1. AppendObjectRequest append = new AppendObjectRequest(testBucket, testObject, uploadFilePath);
  2. ObjectMetadata metadata = new ObjectMetadata();
  3. metadata.setContentType("application/octet-stream");
  4. append.setMetadata(metadata);
  5. // 设置追加位置
  6. append.setPosition(0);
  7. append.setProgressCallback(new OSSProgressCallback<AppendObjectRequest>() {
  8.     @Override
  9.     public void onProgress(AppendObjectRequest request, long currentSize, long totalSize) {
  10.         Log.d("AppendObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
  11.     }
  12. });
  13. OSSAsyncTask task = oss.asyncAppendObject(append, new OSSCompletedCallback<AppendObjectRequest, AppendObjectResult>() {
  14.     @Override
  15.     public void onSuccess(AppendObjectRequest request, AppendObjectResult result) {
  16.         Log.d("AppendObject", "AppendSuccess");
  17.         Log.d("NextPosition", "" + result.getNextPosition());
  18.     }
  19.     @Override
  20.     public void onFailure(AppendObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  21.         // 异常处理
  22.     }
  23. });

用户使用Append方式上传,关键要对Position这个参数进行正确的设置:

  • 当用户创建一个Appendable Object时,追加位置设为0。

  • 当对Appendable Object进行内容追加时,追加位置设为Object当前长度。有两种方式获取该Object长度:通过上传追加后的返回内容获取。
  • 通过head object获取文件长度。




上传后回调通知


客户端在上传Object时可以指定OSS服务端在处理完上传请求后,通知您的业务服务器,在该服务器确认接收了该回调后将回调的结果返回给客户端。因为加入了回调请求和响应的过程,相比简单上传,使用回调通知机制一般会导致客户端花费更多的等待时间。
具体说明参考: Callback
代码示例:
  1. PutObjectRequest put = new PutObjectRequest(testBucket, testObject, uploadFilePath);
  2. put.setCallbackParam(new HashMap<String, String>() {
  3.     {
  4.         put("callbackUrl", "110.75.82.106/callback");
  5.         put("callbackHost", "oss-cn-hangzhou.aliyuncs.com");
  6.         put("callbackBodyType", "application/json");
  7.         put("callbackBody", "{\"mimeType\":${mimeType},\"size\":${size}}");
  8.     }
  9. });
  10. OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
  11.     @Override
  12.     public void onSuccess(PutObjectRequest request, PutObjectResult result) {
  13.         Log.d("PutObject", "UploadSuccess");
  14.         // 只有设置了servercallback,这个值才有数据
  15.         String serverCallbackReturnJson = result.getServerCallbackReturnBody();
  16.         Log.d("servercallback", serverCallbackReturnJson);
  17.     }
  18.     @Override
  19.     public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  20.         // 异常处理
  21.     }
  22. });

如果需要支持自定义参数,参考如下设置:
  1. put.setCallbackParam(new HashMap<String, String>() {
  2.     {
  3.         put("callbackUrl", "http://182.92.192.125/leibin/notify.php");
  4.         put("callbackHost", "oss-cn-hangzhou.aliyuncs.com");
  5.         put("callbackBodyType", "application/json");
  6.         put("callbackBody", "{\"object\":${object},\"size\":${size},\"my_var1\":${x:var1},\"my_var2\":${x:var2}}");
  7.     }
  8. });
  9. put.setCallbackVars(new HashMap<String, String>() {
  10.     {
  11.         put("x:var1", "value1");
  12.         put("x:var2", "value2");
  13.     }
  14. });






展开
收起
青衫无名 2017-10-18 11:54:26 1646 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载