OssUpload.java 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package com.easemob.easeui.oss;
  2. import android.content.Context;
  3. import android.util.Log;
  4. import com.alibaba.sdk.android.oss.ClientConfiguration;
  5. import com.alibaba.sdk.android.oss.ClientException;
  6. import com.alibaba.sdk.android.oss.OSS;
  7. import com.alibaba.sdk.android.oss.OSSClient;
  8. import com.alibaba.sdk.android.oss.ServiceException;
  9. import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
  10. import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
  11. import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
  12. import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
  13. import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
  14. import com.alibaba.sdk.android.oss.model.PutObjectRequest;
  15. import com.alibaba.sdk.android.oss.model.PutObjectResult;
  16. /**
  17. * Created by 程鑫磊 on 2016/11/17.
  18. * on 2016/11/17.
  19. */
  20. public class OssUpload {
  21. OnResponseListener onResponseListener;
  22. public void setOnResponseListener(OnResponseListener onResponseListener) {
  23. this.onResponseListener = onResponseListener;
  24. }
  25. public void ossUpload(Context context, final String path,String name){
  26. String endpoint = "http://img.lesgoapp.cc";
  27. // 明文设置secret的方式建议只在测试时使用,更多鉴权模式请参考后面的`访问控制`章节
  28. OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider("LTAIvkmoS1lN2mCd", "3KLqFPW20hpkXUmFvBwY70P5sxiDcG");
  29. ClientConfiguration conf = new ClientConfiguration();
  30. conf.setConnectionTimeout(25 * 1000); // 连接超时,默认15秒
  31. conf.setSocketTimeout(60 * 1000); // socket超时,默认15秒
  32. conf.setMaxConcurrentRequest(9); // 最大并发请求书,默认5个
  33. conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
  34. OSS oss = new OSSClient(context, endpoint, credentialProvider, conf);
  35. // 构造上传请求
  36. PutObjectRequest put = new PutObjectRequest("lesgo", name, path);
  37. // 异步上传时可以设置进度回调
  38. put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
  39. @Override
  40. public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
  41. Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
  42. }
  43. });
  44. OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
  45. @Override
  46. public void onSuccess(PutObjectRequest request, PutObjectResult result) {
  47. Log.d("PutObject", "UploadSuccess");
  48. Log.d("ETag", result.getETag());
  49. Log.d("RequestId", result.getRequestId());
  50. if(onResponseListener != null){
  51. onResponseListener.sussce(path);
  52. }
  53. }
  54. @Override
  55. public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
  56. // 请求异常
  57. if (clientExcepion != null) {
  58. // 本地异常如网络异常等
  59. clientExcepion.printStackTrace();
  60. if(onResponseListener != null){
  61. onResponseListener.sussce("本地异常");
  62. }
  63. }
  64. if (serviceException != null) {
  65. // 服务异常
  66. Log.e("ErrorCode", serviceException.getErrorCode());
  67. Log.e("RequestId", serviceException.getRequestId());
  68. Log.e("HostId", serviceException.getHostId());
  69. Log.e("RawMessage", serviceException.getRawMessage());
  70. if(onResponseListener != null){
  71. onResponseListener.sussce("服务异常");
  72. }
  73. }
  74. }
  75. });
  76. }
  77. public interface OnResponseListener{
  78. public void sussce(Object obj);
  79. public void error(Object... obj);
  80. }
  81. }