java多张图片上传安卓,Android Rxjava+Retrofit2 多图片+文字上传

注意点:

方案一中:map.put(“files\”; filename=\”” + file.getName(), requestBody);

方案二中:builder.addFormDataPart(“files”, file.getName(), imageBody);

需与服务器一制,如:Content-Disposition: form-data; name=”files”; filename=”1.jpg”)

方案一:

@Multipart

@POST("upload/v1/images")

Observable> uploadImages(@Header("token") String token, @Part("appkey") RequestBody appkey, @Part("sign") RequestBody sign,

@PartMap() Mapmap);

/**

* 上传

* @param compressFile 需要上传的文件

*/

private void upload(ListcompressFile) {

LogUtil.e(TAG, "uploadImages: 开始上传...");

//多张图片

Mapmap = new HashMap<>();

if (compressFile != null && !compressFile.isEmpty()) {

for (int i = 0; i < compressFile.size(); i++) {

File file = compressFile.get(i);

LogUtil.e(TAG, "upload: path: " + file.getAbsolutePath());

RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);

map.put("files\"; filename=\"" + file.getName(), requestBody);

}

}

RequestBody appkey = RequestBody.create(null, Api.APPKEY);

RequestBody sign = RequestBody.create(null, Api.SIGN);

mApiWrapper.uploadImages(appkey, sign, map)

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(new Observer() {

@Override

public void onSubscribe(@NonNull Disposable d) {

}

@Override

public void onNext(@NonNull Object t) {

LogUtil.e(TAG, "uploadImages: 上传成功");

}

@Override

public void onError(@NonNull Throwable e) {

}

@Override

public void onComplete() {

}

});

}

方案二:

@Multipart

@POST("upload/v1/images")

Observable> uploadImages1(@Header("token") String token, @Part ListpartList);

/**

* 上传

* @param compressFile 需要上传的文件

*/

private void upload(ListcompressFile) {

LogUtil.e(TAG, "uploadImages: 开始上传...");

//多张图片

MultipartBody.Builder builder = new MultipartBody.Builder()

.setType(MultipartBody.FORM)//表单类型

.addFormDataPart("appkey", Api.APPKEY)

.addFormDataPart("sign", Api.SIGN);

for (int i = 0; i < compressFile.size(); i++) {

File file = compressFile.get(i);

RequestBody imageBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);

builder.addFormDataPart("files", file.getName(), imageBody);

}

Listparts = builder.build().parts();

mApiWrapper.uploadImages(parts)

.subscribeOn(Schedulers.io())

.observeOn(AndroidSchedulers.mainThread())

.subscribe(new Observer() {

@Override

public void onSubscribe(@NonNull Disposable d) {

}

@Override

public void onNext(@NonNull Object t) {

LogUtil.e(TAG, "uploadImages: 上传成功");

}

@Override

public void onError(@NonNull Throwable e) {

}

@Override

public void onComplete() {

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值