12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package com.easemob.easeui.oss;
- import android.content.Context;
- import android.util.Log;
- import com.alibaba.sdk.android.oss.ClientConfiguration;
- import com.alibaba.sdk.android.oss.ClientException;
- import com.alibaba.sdk.android.oss.OSS;
- import com.alibaba.sdk.android.oss.OSSClient;
- import com.alibaba.sdk.android.oss.ServiceException;
- import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
- import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
- import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider;
- import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider;
- import com.alibaba.sdk.android.oss.internal.OSSAsyncTask;
- import com.alibaba.sdk.android.oss.model.PutObjectRequest;
- import com.alibaba.sdk.android.oss.model.PutObjectResult;
- /**
- * Created by 程鑫磊 on 2016/11/17.
- * on 2016/11/17.
- */
- public class OssUpload {
- OnResponseListener onResponseListener;
- public void setOnResponseListener(OnResponseListener onResponseListener) {
- this.onResponseListener = onResponseListener;
- }
- public void ossUpload(Context context, final String path,String name){
- String endpoint = "http://img.lesgoapp.cc";
- // 明文设置secret的方式建议只在测试时使用,更多鉴权模式请参考后面的`访问控制`章节
- OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider("LTAIvkmoS1lN2mCd", "3KLqFPW20hpkXUmFvBwY70P5sxiDcG");
- ClientConfiguration conf = new ClientConfiguration();
- conf.setConnectionTimeout(25 * 1000); // 连接超时,默认15秒
- conf.setSocketTimeout(60 * 1000); // socket超时,默认15秒
- conf.setMaxConcurrentRequest(9); // 最大并发请求书,默认5个
- conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
- OSS oss = new OSSClient(context, endpoint, credentialProvider, conf);
- // 构造上传请求
- PutObjectRequest put = new PutObjectRequest("lesgo", name, path);
- // 异步上传时可以设置进度回调
- put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
- @Override
- public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
- Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
- }
- });
- OSSAsyncTask task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
- @Override
- public void onSuccess(PutObjectRequest request, PutObjectResult result) {
- Log.d("PutObject", "UploadSuccess");
- Log.d("ETag", result.getETag());
- Log.d("RequestId", result.getRequestId());
- if(onResponseListener != null){
- onResponseListener.sussce(path);
- }
- }
- @Override
- public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
- // 请求异常
- if (clientExcepion != null) {
- // 本地异常如网络异常等
- clientExcepion.printStackTrace();
- if(onResponseListener != null){
- onResponseListener.sussce("本地异常");
- }
- }
- if (serviceException != null) {
- // 服务异常
- Log.e("ErrorCode", serviceException.getErrorCode());
- Log.e("RequestId", serviceException.getRequestId());
- Log.e("HostId", serviceException.getHostId());
- Log.e("RawMessage", serviceException.getRawMessage());
- if(onResponseListener != null){
- onResponseListener.sussce("服务异常");
- }
- }
- }
- });
- }
- public interface OnResponseListener{
- public void sussce(Object obj);
- public void error(Object... obj);
- }
- }
|