七牛批量上传
安装七牛SDK,因为我们项目需要每个文件必须要一个token,也可以获取一个token然后客户端自己用拼接key
注:如果自己拼接key最后是时间戳加上图片的下标,不然容易因为上传时间过快导致时间戳一样,造成上传失败。当初写iOS上传自己拼接key的时候遇到过这个问题。
创建一个工具类QiNiuUploadUtil,
初始化七牛的上传工具类UploadManager
然后创建回调接口QiNiuUploadCompletionHandler
private static UploadManager uploadManager = new UploadManager();
private ArrayList pathList = new ArrayList<>();
//记录当前图片下标
private int imageIndex;
//用于上传数据集合的时候回调
private QiNiuUploadCompletionHandler completionHandler;
public void setCompletionHandler(QiNiuUploadCompletionHandler completionHandler) {
this.completionHandler = completionHandler;
}
上传单个文件
public void uploadSingle(final String path, int type, final String dataType, final QiNiuUploadCompletionHandler handler){
//自己写的请求七牛token方法,因为我们项目需要每一个文件必须要一个token。
HttpClient.getQiniuToken(type, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@Override
public void onSuccess(JSONObject response) {
final QiniuBean qiniuBean = new Gson().fromJson(response.toString(), QiniuBean.class);
final String key = qiniuBean.getKey() + "." + dataType;
//调用七牛上传方法
uploadManager.put(path, key, qiniuBean.getToken(),
new UpCompletionHandler() {
@Override
public void complete(String s, ResponseInfo responseInfo, JSONObject jsonObject) {
if (responseInfo.isOK()) {
if (handler != null){
String path = qiniuBean.getDomain() + "/" + key;
handler.uploadSuccess(path,null);
}
} else {
if (handler != null){
handler.uploadFailure();
}
}
}
},null);
}
@Override
public void onFault(String errorMsg) {
if (handler != null){
handler.uploadFailure();
}
}
}));
}
第一种上传多个文件,用递归方法
因为本项目图片多选都用PictureSelector选择器所提上传图片数组 类型为LocalMedia,这个根据自己项目来改
public void uploadImageArray(final List imageArray,final int index){
imageIndex = index;
if (index == imageArray.size()){
if (completionHandler != null){
completionHandler.uploadSuccess(null, pathList);
}
}else {
//获取token
HttpClient.getQiniuToken(0, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@Override
public void onSuccess(final JSONObject response) {
final QiniuBean qiniuBean = new Gson().fromJson(response.toString(), QiniuBean.class);
LocalMedia localMedia = imageArray.get(index);
final String key = qiniuBean.getKey() + "." + localMedia.getPictureType();
//调用七牛上传图片API
uploadManager.put(localMedia.getPath(), key, qiniuBean.getToken(),
new UpCompletionHandler() {
@Override
public void complete(String s, ResponseInfo responseInfo, JSONObject jsonObject) {
if (responseInfo.isOK()) {
String path = qiniuBean.getDomain() + "/" + key;
pathList.add(path);
imageIndex++;
uploadImageArray(imageArray, imageIndex);
} else {
if (completionHandler != null){
completionHandler.uploadFailure();
}
}
}
},null);
}
@Override
public void onFault(String errorMsg) {
if (completionHandler != null){
completionHandler.uploadFailure();
}
}
}));
}
}
回调接口
public interface QiNiuUploadCompletionHandler{
//上传数组时候list有值,单个上传时候path有值
void uploadSuccess(String path, ArrayList paths);
void uploadFailure();
}
第二种用循环上传
/**
* 上传图片数组 类型为LocalMedia,因为本项目图片多选都用PictureSelector选择器
* @param imageArray 图片数组
* @param index
*
*/
public void uploadImageArray(final List imageArray, final int index){
imageIndex = index;
for (final LocalMedia localMedia : imageArray){
//自己写的请求七牛token方法,因为我们项目需要每一个文件必须要一个token。
HttpClient.getQiniuToken(0, new OnSuccessAndFaultSub(new OnSuccessAndFaultListener() {
@Override
public void onSuccess(final JSONObject response) {
final QiniuBean qiniuBean = new Gson().fromJson(response.toString(), QiniuBean.class);
final String key = qiniuBean.getKey() + "." + localMedia.getPictureType();
//调用七牛上传图片API
uploadManager.put(localMedia.getCompressPath(), key, qiniuBean.getToken(),
new UpCompletionHandler() {
@Override
public void complete(String s, ResponseInfo responseInfo, JSONObject jsonObject) {
if (responseInfo.isOK()) {
String path = qiniuBean.getDomain() + "/" + key;
pathList.add(path);
imageIndex++;
//如果成功的次数等于数组个数测表示全部上传成功
if (imageIndex == imageArray.size()){
if (completionHandler != null){
completionHandler.uploadSuccess(null, pathList);
}
}
} else {
if (completionHandler != null){
completionHandler.uploadFailure();
}
}
}
},null);
}
@Override
public void onFault(ErrorBean errorMsg) {
if (completionHandler != null){
completionHandler.uploadFailure();
}
}
}));
}
}