我有一个很大的文件下载,由一个服务器上的RestController提供服务,我需要通过另一个服务器上的RestController进行流式传输。当直接调用终端服务器时,结果流正常。但是,当使用RestTemplate调用此服务器,然后将响应写入OutputStream时,响应将在前端服务器上进行缓冲,直到整个文件准备就绪,然后再进行流式传输。有没有一种方法可以在文件传入时将其写入OutputStream?
目前,我在前端服务器上的代码如下所示
@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);
}我已经将我的RestTemplate设置为not buffer,并且通过检查所使用的请求类型(SimpleStreamingClientHttpRequest)来验证它是否正常工作。数据返回时都是正确的,它只是一次全部写入流中,而不是直接写入
发布于 2020-02-26 21:13:09
您可以使用restTemplate.execute。请参阅https://www.baeldung.com/spring-resttemplate-download-large-file
发布于 2017-02-15 05:34:00
与pointed out in this JIRA issue一样,RestTemplate不是用于流式传输响应正文的。
https://stackoverflow.com/questions/37939867
复制相似问题