I have a client that needs to send POST a large number of large json files to the server. I was able to get it working by reading each of the files in memory and sending the whole file using RestTemplate. However, the client quickly runs out of memory associated with large json files. I want to switch to the streaming approach, but I cannot figure out how to use FileInputStream with RestTemplate correctly. I found this question and used the code provided in the accepted answer, but I still see the memory usage and OutOfMemory exceptions that make me think that this is not streaming files but still read them completely. What am I doing wrong? Here is what I have at the moment:
final InputStream fis = ApplicationStore.class.getResourceAsStream(path); final RequestCallback requestCallback = new RequestCallback() { @Override public void doWithRequest(final ClientHttpRequest request) throws IOException { request.getHeaders().add("Content-type", "application/json"); IOUtils.copy(fis, request.getBody()); } }; final RestTemplate restTemplate = new RestTemplate(); SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setBufferRequestBody(false); restTemplate.setRequestFactory(requestFactory); final HttpMessageConverterExtractor<String> responseExtractor = new HttpMessageConverterExtractor<String>(String.class, restTemplate.getMessageConverters()); restTemplate.execute("http://" + host + ":8080/upads-data-fabric" + "/ruleset", httpMethod, requestCallback, responseExtractor);
source share