I have a large file upload that RestController serves on one server, which I need to transfer through RestController on another server. When directly calling the destination server, the result streams are excellent. However, when using RestTemplate to call this server and then record the response to the OutputStream, the response is buffered on the front server until the entire file is ready, and then streamed. Is there a way to write a file to OutputStream when it comes in?
At the moment, my code on the front server is similar to this
@ResponseBody
public void downloadResults(HttpServletRequest request, HttpServletResponse response, @RequestParam("id") String jobId, OutputStream stream)
throws IOException
{
byte[] data = restTemplate.exchange("http://localhost/getFile", HttpMethod.POST, requestEntity, byte[].class, parameters).getBody();
stream.write(data);
}
I found that my RestTemplate is not buffered, and I verified that this works by checking the type of request used (SimpleStreamingClientHttpRequest). All data is correct, it is simply written only to the stream at once, and not to
source
share