I have a servlet which is able to receive binary data [files].
I would like to use Spring's RestTemplate() to upload a large binary file to the servlet. However its not possible to load the binary file entirely into memory.
So far my attempts have resulted in OutOfMemory errors, indicating the methods have been attempting to load the entire file into memory.
How can I stream this binary data to the servlet? Preferable in Spring or otherwise in Java.
Uploading a Single File We need to create HttpEntitywith header and body. Set the content-type header value to MediaType. MULTIPART_FORM_DATA. When this header is set, RestTemplate automatically marshals the file data along with some metadata.
Found the answer:
https://jira.springsource.org/browse/SPR-7909
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setBufferRequestBody(false);
RestTemplate rest = new RestTemplate(requestFactory);
This prevents loading the entire request into memory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With