1. 程式人生 > >Android Rxjava+Retrofit2 多圖片+文字上傳

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<HttpResult<String>> uploadImages(@Header("token") String token, @Part("appkey") RequestBody appkey, @Part("sign") RequestBody sign,
                                            @PartMap() Map<String, RequestBody> map);

/**
 * 上傳
 * @param compressFile 需要上傳的檔案
 */
private void upload(List<File> compressFile) {
    LogUtil.e(TAG, "uploadImages: 開始上傳...");
    //多張圖片
    Map<String, RequestBody> map = 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<HttpResult<String>> uploadImages1(@Header("token") String token, @Part List<MultipartBody.Part> partList);

/**
 * 上傳
 * @param compressFile 需要上傳的檔案
 */
private void upload(List<File> compressFile) {
    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);
    }
    List<MultipartBody.Part> parts = 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() {

         }
});