retrofit2 file upload

  • 1. Single file upload interface declaration:
 @Multipart
 @POST("api/Setting/")
 Call<ServerResponse<AvatarBean>> updateAva(
         @Part MultipartBody.Part img
 ); 

File upload must be accompanied by @Multipart comment parameter with @Part comment
Interface Request:

RequestBody photoRequestBody =RequestBody.create(MediaType.parse("image/jpg"), imgFile);
MultipartBody.Part part = MultipartBody.Part.createFormData("avatar", imgFile.getName(),
    photoRequestBody);
    mProfileService.updateAva(part).enqueue(new Callback<ServerResponse<AvatarBean>>() {
            @Override
            public void onResponse(@NonNull Call<ServerResponse<AvatarBean>> call, @NonNull Response<ServerResponse<AvatarBean>> response) {
                ///
            }

            @Override
            public void onFailure(@NonNull Call<ServerResponse<AvatarBean>> call, @NonNull Throwable t) {
                ///
            }
        });


    }

MultipartBody.Part.createFormDataThe three parameters of () are
The key of the upload file, the name of the upload file, and the RequestBody generated by the upload file note that the @Part comment cannot be followed by a parameter name

  • 2. File Array Upload
    Interface definition:
//Upload pictures to Aliyun, universal picture upload interface
    @Multipart
    @POST("api/UploadImage/")
    Call<ServerResponse<List<String>>> uploadImage(
            @Part List<MultipartBody.Part> files
    );

Similar to single file uploads, use @Multipart with the @POST comment and @Part for parameters
Request call:

List<MultipartBody.Part> parts = createFileParts(listImagePath);
mJuService.uploadImage(parts).enqueue(new Callback<ServerResponse<List<String>>>() {
         public void onResponse(Call<ServerResponse<List<String>>> call, Response<ServerResponse<List<String>>> response) {
         //Response Content
         }
         @Override
         public void onFailure(@NonNull Call<SimpleServerResponse> call, @NonNull Throwable t) {
         //Request Error Handling
         }      
}
@NonNull
    private List<MultipartBody.Part> createFileParts(@NonNull List<String> listImagePath) {
        List<MultipartBody.Part> parts = new ArrayList<>();
        for (String path : listImagePath) {
            File file = new File(path);
            RequestBody body = RequestBody.create(MediaType.parse("image/" +    path.substring(path.lastIndexOf(".") + 1)), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("images[]", file.getName(), body);//Note that while the document says key is image, the android side must use images[] as key to upload multiple files successfully
            parts.add(part);
        }
        return parts;
    }

Note: 1. If multi-parameter single file upload is similar to single file upload, add a few parameters to it

@Multipart
@POST("api/Setting/")
Call<ServerResponse<AvatarBean>> updateAva(
        @Part MultipartBody.Part fiel1,
        @Part MultipartBody.Part fiel2,
        @Part MultipartBody.Part fiel3,
        @Part MultipartBody.Part fiel4
); 
   2. Use if additional parameters are required to upload files
@Multipart
    @POST("store/provider/add-comment/")
    Call<SimpleServerResponse> addCompanyCommen(
            @Query("provider_id") String provider_id,//General parameters
            @Part List<MultipartBody.Part> files //File Array
   );

If you upload indefinite parameters

    @POST("store/provider/add-comment/")
    Call<SimpleServerResponse> addCompanyCommen(
            @QueryMap Map<String, String> argMap, //Normal parameter map (key-value pair is the key-value pair to upload)
            @Part List<MultipartBody.Part> files //File Array
    );

Tags: Android

Posted on Thu, 04 Jun 2020 10:39:33 -0700 by mmarif4u