日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發布時間:2023/12/10 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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() {

}

});

總結

以上是生活随笔為你收集整理的java多张图片上传安卓,Android Rxjava+Retrofit2 多图片+文字上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。