Upload file with Retrofit 2

后端 未结 2 1290
名媛妹妹
名媛妹妹 2021-02-06 19:08

I trying to do that a few days, and I really did everything.. Here is how request looks in Postman:

I am sure, that all GET parameters were writing correctly.

相关标签:
2条回答
  • 2021-02-06 20:02

    Upload Image See Here click This Link

    http://mushtaq.16mb.com/retrofit_example/uploads/

    import retrofit2.Retrofit;
    import retrofit2.converter.gson.GsonConverterFactory;
    
    class AppConfig {
    
        private static String BASE_URL = "http://mushtaq.16mb.com/";
    
        static Retrofit getRetrofit() {
    
            return new Retrofit.Builder()
                    .baseUrl(AppConfig.BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
    }
    
    ========================================================
    import okhttp3.MultipartBody;
    import okhttp3.RequestBody;
    import retrofit2.Call;
    import retrofit2.http.Multipart;
    import retrofit2.http.POST;
    import retrofit2.http.Part;
    
    interface ApiConfig {
        @Multipart
        @POST("retrofit_example/upload_image.php")
        Call<ServerResponse> uploadFile(@Part MultipartBody.Part file,
                                        @Part("file") RequestBody name);
    
        /*@Multipart
        @POST("ImageUpload")
        Call<ServerResponseKeshav> uploadFile(@Part MultipartBody.Part file,
                                        @Part("file") RequestBody name);*/
    
        @Multipart
        @POST("retrofit_example/upload_multiple_files.php")
        Call<ServerResponse> uploadMulFile(@Part MultipartBody.Part file1,
                                           @Part MultipartBody.Part file2);
    }
    
    
    
    
    
    
    https://drive.google.com/open?id=0BzBKpZ4nzNzUMnJfaklVVTJkWEk
    
    0 讨论(0)
  • 2021-02-06 20:03

    I spent a lot of time to search, how to send a file as byte-stream because all answers in web explain upload via RequestBody, but it's not working for my case. So, here is the solution:

    InputStream in = new FileInputStream(file);
        byte[] buf = new byte[in.available()];
        while (in.read(buf) != -1) ;
        RequestBody requestBodyByte = RequestBody
                .create(MediaType.parse("application/octet-stream"), buf);
        String content_disposition = "attachment; filename=\"" + fileName + "\"";
    
        //GET parameters
        Map<String, String> params = new HashMap<String, String>();
        params.put("inspectionUUID", inspectionUUID);
        params.put("noteUUID", noteUUID);
        params.put("attachmentUUID", attachmentUUID);
        params.put("noteType", noteType);
        params.put("modifiedTime", modifiedTime);
    
        Call<ResponseBody> call = service.upload(access_token, content_disposition, requestBodyByte, params);
    

    Interface:

     @POST("api/MediaFiles/AddMediaFile")
        Call<ResponseBody> upload(
                @Header("Authorization") String authorization,@Header("Content-Disposition") String content_disposition, @Body RequestBody photo,
                /* GET params */ @QueryMap Map<String, String> params
        );
    
    0 讨论(0)
提交回复
热议问题