AWS S3 Rest API with Android Retrofit V2 library, uploaded image is damaged

后端 未结 6 1576
生来不讨喜
生来不讨喜 2020-12-03 08:57

I\'m trying upload a Image from my Android APP to Amazon AWS S3 and I need use AWS Restful API.

I\'m using Retrofit 2

6条回答
  •  囚心锁ツ
    2020-12-03 09:45

    I have the same problem, and as I use Fiddler checked the HTTP request content, I found retrofit 2.0.0 beta1 has a different with 1.9.0.

    In my problem, the different of HTTP request content prevent server get the correct data.

    In order to make a same HTTP request content, i do next steps using retrofit 2.0.0 deta1.


    In the retrofit service, add a form-data header for the http request;

    @Headers("Content-Type: multipart/form-data;boundary=95416089-b2fd-4eab-9a14-166bb9c5788b")
    

    int retrofit 2.0.0 deta1, the header using @Multipart will get a data like this:

    Content-Type: multipart/mixed

    as the deafult value is mixed, and has no boundary title.


    Do not using @Multipart to upload file, just using @Body RequestBody

    if you using @Multipart to request Server, you have to pass param(file) through

    @Part(key), then a new problem you will get. May be retrofit 2.0.0beta1 has a BUG ..., @Multipart generate a bad http request compile with 1.9.0.


    When you call the method, you need pass MultipartRequestBody to @Body RequestBody

    Using MultipartBuilder to create a MultipartRequestBody, when you new MultipartBuilder, call this consturt:

    new MultipartBuilder("95416089-b2fd-4eab-9a14-166bb9c5788b")
    

    the param is you set int @headers(boundary=)

    builder.addFormDataPart(String name, String filename, RequestBody value)
    

    This method will help form a data like below int HTTP request content:

    Content-Disposition: form-data; name="imgFile"; filename="IMG_20150911_113029.jpg" Content-Type: image/jpg Content-Length: 1179469

    RequestBody value is what you has generate in your code.

    I just resolve this problem temporary.

    Hope can help you!

提交回复
热议问题