Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload large binary file to using Java Spring RestTemplate

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.

like image 755
Hylton Peimer Avatar asked Dec 06 '12 18:12

Hylton Peimer


People also ask

How do I upload a file using RestTemplate in Spring boot?

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.


1 Answers

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.

like image 122
Hylton Peimer Avatar answered Sep 29 '22 17:09

Hylton Peimer