与ios的阿里云上传图片类似,目前是也刚接触android开发时间不长,在此记录一下
1.从后台服务器获取keyId、keySecret、token
此处省略。我们后台同时返回给我path图片在阿里云存储位置
2.android代码实现
2.1第一步先实现一个数据返回接口
import com.alibaba.sdk.android.oss.model.PutObjectResult;
public interface AliyunOSSCallBack {
public void oSSRequestSuccess(PutObjectResult object);
public void oSSRequestFailure(String errorCode,String msg);
}
2.2代码实现
public class AliyunOSSUtils {
// 访问的endpoint地址
public static final String OSS_ENDPOINT = "http://oss-cn-zhangjiakou.aliyuncs.com";
public static final String BUCKET_NAME = "test";
private static OSS oss;
protected AliyunOSSCallBack ossCallBack;
private OSSAsyncTask task;
public void setOsstoken(String keyid, String keysecret, String token) {
OSSCredentialProvider provider = new OSSStsTokenCredentialProvider(keyid, keysecret,token);
ClientConfiguration conf = new ClientConfiguration();
conf.setConnectionTimeout(15 * 1000); // 连接超时,默认15秒
conf.setSocketTimeout(15 * 1000); // socket超时,默认15秒
conf.setMaxConcurrentRequest(5); // 最大并发请求书,默认5个
conf.setMaxErrorRetry(2); // 失败后最大重试次数,默认2次
oss = new OSSClient(MainApplication.getAppCtx(), OSS_ENDPOINT, provider, conf);
}
/**
* 同步上传单个文件
*
* @param name
* @param localPath
*/
public void uploadFile(String name, String localPath,final AliyunOSSCallBack callBack) {
ossCallBack = callBack;
// 构造上传请求。
PutObjectRequest put = new PutObjectRequest(BUCKET_NAME, name, localPath);
try {
PutObjectResult putResult = oss.putObject(put);
ossCallBack.oSSRequestSuccess(putResult);
} catch (ClientException e) {
// 本地异常,如网络异常等。
e.printStackTrace();
ossCallBack.oSSRequestFailure("",e.getMessage());
} catch (ServiceException e) {
// 服务异常。
ossCallBack.oSSRequestFailure(e.getErrorCode(),e.getRawMessage());
}
}
/**
* 异步上传单个文件
*
* @param name
* @param localPath
*/
public void asynploadFile(String name, String localPath) {
// 构造上传请求。
PutObjectRequest put = new PutObjectRequest(BUCKET_NAME, name, localPath);
// 异步上传时可以设置进度回调。
put.setProgressCallback(new OSSProgressCallback<PutObjectRequest>() {
@Override
public void onProgress(PutObjectRequest request, long currentSize, long totalSize) {
Log.d("PutObject", "currentSize: " + currentSize + " totalSize: " + totalSize);
}
});
task = oss.asyncPutObject(put, new OSSCompletedCallback<PutObjectRequest, PutObjectResult>() {
@Override
public void onSuccess(PutObjectRequest request, PutObjectResult result) {
Log.d("PutObject", "UploadSuccess");
ossCallBack.oSSRequestSuccess(result);
}
@Override
public void onFailure(PutObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
// Request exception
if (clientExcepion != null) {
// Local exception, such as a network exception
clientExcepion.printStackTrace();
ossCallBack.oSSRequestFailure("",clientExcepion.getMessage());
}
if (serviceException != null) {
ossCallBack.oSSRequestFailure(serviceException.getErrorCode(),serviceException.getRawMessage());
}
}
});
// task.cancel(); // Cancel the task
// task.waitUntilFinished(); // Wait till the task is finished
}
/**
* 取消异步请求
*
* @param
*/
public void asynCancel(){
if(task != null && task.isCanceled()){
task.cancel();
}
}
}
网友评论