6

I am trying to upload a 260k image file as part of multipart form using Apache HttpAsyncClient library.

I create my form this way:

val multipartEntityBuilder = new MultipartEntityBuilder
multipartEntityBuilder.addBinaryBody("file", file)
val multipartEntity = multipartEntityBuilder.build()

And then I receive a ContentTooLongException when performing request basically because of this line in the library's source code: https://github.com/apache/httpclient/blob/4.5.3/httpmime/src/main/java/org/apache/http/entity/mime/MultipartFormEntity.java#L102

I searched a lot, but didn't find any explanation why this limitation for contentLength is present in code. Maybe someone could explain it? And my second question: what is the proper way to make an upload request for a file larger than 25 kb?

Thanks!

2
  • Updated link: github.com/apache/httpcomponents-client/blob/master/httpclient5/… And explanation: issues.apache.org/jira/browse/HTTPASYNC-163 Commented May 26, 2021 at 6:43
  • And the suggestion is to indeed use the BufferedHttpEntity: One should either build a custom `HttpAsyncRequestProducer` capable of producing multipart content asynchronously or wrap `MultipartFormEntity` with `BufferedHttpEntity` if they do not mind buffering multipart content in memory prior to writing it out to an asynchronous I/O channel. Commented May 26, 2021 at 6:44

1 Answer 1

10

Found the solution: create inputStream over file and wrap the multipart entity with BufferedHttpEntity and then pass this buffered entity to request:

val multipartEntityBuilder = MultipartEntityBuilder.create()
multipartEntityBuilder.addBinaryBody("file", new FileInputStream(file), ContentType.DEFAULT_BINARY, name)
val multipartEntity = multipartEntityBuilder.build()
val entity = new BufferedHttpEntity(multipartEntity)
Sign up to request clarification or add additional context in comments.

1 Comment

This loads the whole file in memory so uploading a 50gb file results in an out of memory error, any way to avoid it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.