Retrofit: Uploading large images using retrofit 2

Created on 30 Oct 2017  ·  11Comments  ·  Source: square/retrofit

Iam using Retrofit 2.3.0 to upload images to a .Net framework server,
the uploading of images i working fine .. ubt when i try to upload >5MB images it gives me an error
when i logged the request it doesn't read the full file and gives EOF exception .. and i do have to compress the image with about 50% quality which is not very good
iam using MultiPart to upload and this is my code snippet

    RequestBody requestFile =
                    RequestBody.create(MediaType.parse("image/jpeg"), file);
            MultipartBody.Part body =
                    MultipartBody.Part.createFormData("image", file.getName(), requestFile);

// add another part within the multipart request
            RequestBody fullName =
                    RequestBody.create(
                            MediaType.parse("multipart/form-data"), image.getDescription());

and this is the okHttp log

                 --------- beginning of main
10-30 14:25:45.330 11236-11402/com.project.peoject D/OkHttp: �Dwm�1l

�n�D�RXZX�1[)$M��l�`��7Q���n�ӟ̊e�L،����=�k���a�~�g��=�U�ɨ@<�x��/���>� ���R��@��z;c�?��Zʖ7��ۮ�0�r����#ך�����3�J��F{���v� ��Kǩ�$||

                                                               read: unexpected EOF!

Most helpful comment

@JakeWharton @Bhoomika06
I am facing same issue if i am sending files for more than 3 mb.
Code is working fine for files size under 2 mb.
Please help me out.

All 11 comments

The behavior you are seeing comes from OkHttp, not Retrofit. But even so, I don't see how the client could be at fault. I've used OkHttp to send gigabytes of data, so I expect the network is breaking, the server is rejecting the request mid-stream, or someone is changing the file out underneath the client.

If you can describe the problem as a failing test case or executable sample we can take a look on the OkHttp issue tracker.

Hello Folk, I am facing the same problem while uploading video file (Size: around 6 MB) along with Image file. When I tried to sent only Video, Still got Same Error. If You can suggest anything to me; Please Give Suggestions.

@JakeWharton @Bhoomika06
I am facing same issue if i am sending files for more than 3 mb.
Code is working fine for files size under 2 mb.
Please help me out.

Any update ? @JakeWharton

@JakeWharton .. Less than 6Mb is working fine. But not able to upload more than 6Mb.
Please help

Check out timeout limit, because it can finish earlier than file is loaded!

OkHttpClient okHttpClient = new OkHttpClient.Builder() .connectTimeout(30, TimeUnit.SECONDS) .addInterceptor(httpLoggingInterceptor) .addInterceptor(new DecriptInterceptor()) .build();

Was this issue somehow resolved? Even I'm can't upload a collective media size of more than 6 MB. Backend is hosted on a hostgator VPS and written in php.

Has any one got the solution . i am facing the same problem . even i have increased my time duration limit. i can upload less then 4mb video file easily, but more then 4mb unable to upload.
httpClient.readTimeout(240, TimeUnit.SECONDS);
httpClient.connectTimeout(240, TimeUnit.SECONDS);
httpClient.writeTimeout(240, TimeUnit.SECONDS);

Yes, There is no issue in the library. My issue is resolved.
Its Server side issue.
You Might check following point to resolve it:-

  1. Check how lengthy file can be uploaded to the server.
  2. Check server connection time out from the server point of view.
  3. Make sure you have to change the file size on Apache server also. (In my case php filesize of apache server was 5MB)

This case happens when sending large attachment and backend has fix max size for attachment usually 5 MB,
Should send the attachment via Chunks technique, and each chunk should have max size.

Ex -> you have a file with size 20 MB and backend max size 5 MB:

  1. should separate this file to 4 chunks (20/5)
  2. send chunks
  3. after sent those chunks successfully handle success and return callbacks

Solution :
https://github.com/square/retrofit/issues/540#issuecomment-46934042

Well, you should try this technique to find a solution on GitHub:

  • Go to retrofit issues section
  • Search with words: logging Interceptor
  • try sort by "no of comments"
  • Read only title of each issue, you will find your question with maximum Answers
    The final query would be
    is:issue logging Interceptor sort:comments-desc
    That's how I found a reliable solution.

Happy 👍 💯 🥇

Was this page helpful?
0 / 5 - 0 ratings