简体   繁体   English

Retrofit2-AWS S3 Multipart image文件损坏问题

[英]Retrofit2-AWS S3 Multipart image File corrupt issue

I am using Retrofit2 for handling API calls in my Android application. 我在我的Android应用程序中使用Retrofit2来处理API调用。 The application contains several image upload features. 该应用程序包含多个图像上传功能。 All the image uploading features are wired up to two API calls and will fire one after another. 所有图像上传功能都连接到两个API调用,并将一个接一个地触发。

  1. The first API will upload the image content to the server and the server will generate a dynamic signed AWS S3 image URL as a response. 第一个API将图像内容上传到服务器,服务器将生成动态签名的AWS S3图像URL作为响应。

  2. The second API is using the above-signed URL and PUT the data into AWS S3. 第二个API使用上面签名的URL并将数据PUT到AWS S3中。

It was working perfectly for small images. 它适用于小图像。 For large images, the API has failed. 对于大图像,API失败。 The possible solution for this issue is to change the upload API as multipart. 此问题的可能解决方案是将上传API更改为multipart。 So I have changed the second API as " Retrofit2-multipart ". 所以我将第二个API改为“ Retrofit2-multipart ”。 But the problem here is, After uploading the image, the file got corrupted in the S3. 但问题是,在上传图像后,文件在S3中被破坏了。

So my question is, Is retrofit multipart is fit for ASW S3 upload? 所以我的问题是,改装multipart是否适合ASW S3上传? Do we have any solution to solve this issue?. 我们有解决这个问题的任何解决方案吗?

Please check my Implementation 请检查我的实施

Retrofit API Interface 改造API接口

 @Multipart
    @PUT
    Call<Void> uploadFile(@Url String url,
                          @Part MultipartBody.Part file);

Retrofit service creator 改造服务创建者

 private ApiServicesList createService() {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .readTimeout(300, TimeUnit.SECONDS)
                .connectTimeout(360, TimeUnit.SECONDS)
                .cache(null)
                .addInterceptor(new NetworkInterceptor(context))
                .addInterceptor(createLoggingInterceptor())
                .addInterceptor(createSessionExpiryInterceptor())
                .addInterceptor(createContextHeaderInterceptor())
                .build();

        return new Retrofit.Builder()
                .baseUrl(FirebaseConfig.getInstance().getStagingBaseURl())
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build()
                .create(ApiServicesList.class);
    }

File Upload Class 文件上传类

 public void uploadFileTos3(String url, MultipartBody.Part file, Callback<Void> listener) {
        Call<Void> call = mAPIServices.uploadFile(url,file);
        call.enqueue(listener);
    }

Presenter API Call Presenter API调用

networkServices.uploadFileTos3(credentials.getSignedUrl(), prepareRequestBody(file), new Callback<Void>() {
            @Override
            public void onFailure(Call<Void> call, Throwable t) {
               //App Action
            }

            @Override
            public void
            onResponse(Call<Void> call, Response<Void> response) {
               //App Action
            }
        });

Request Body Method 请求身体方法

 public static MultipartBody.Part prepareRequestBody(File file) {
        RequestBody requestFile = RequestBody.create(MediaType.parse(CONTENT_TYPE), file); // Checked Both "image/jpeg" and "multipart/form-data"
        return  MultipartBody.Part.createFormData("image", file.getName(), requestFile);
    }

DDMS LOGS DDMS日志

2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: --> PUT https:Signed URL(Removed actual URL)
2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: Content-Type: multipart/form-data; boundary=60561d1c-ff3f-4a43-8022-ca2be3e8ec4e
2019-04-10 19:07:13.926 18761-18874/com.xxx D/OkHttp: Content-Length: 71154
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: --60561d1c-ff3f-4a43-8022-ca2be3e8ec4e
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Disposition: form-data; name="image"; filename="forest-trees-fog-foggy.jpg"
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Type: image/jpeg
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Content-Length: 70934
2019-04-10 19:07:13.931 18761-18874/com.xxx D/OkHttp: Image Body
2019-04-10 19:07:13.942 18761-18874/com.xxx D/OkHttp: --60561d1c-ff3f-4a43-8022-ca2be3e8ec4e--
2019-04-10 19:07:13.942 18761-18874/com.xxx D/OkHttp: --> END PUT (71154-byte body)
2019-04-10 19:07:15.648 18761-18874/com.xxx D/OkHttp: <-- 200 OK

Try chaging the call to createFormData with "file" instead of "image": 尝试使用“file”而不是“image”来调用createFormData的调用:

    public static MultipartBody.Part prepareRequestBody(File file) {
        RequestBody requestFile = RequestBody.create(MediaType.parse(CONTENT_TYPE), file); // Checked Both "image/jpeg" and "multipart/form-data"
        return  MultipartBody.Part.createFormData("file", file.getName(), requestFile);
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM