我有一个下载大尺寸 zip 文件的 url。它以流的形式返回响应。虽然文件大小首先返回 200(HTTPSTATUK.OK) 并继续下载。
我必须实现一个新的spring控制器,它通过rest模板调用上面的url。我必须读取rest模板返回的响应并传递给控制器。最初我已经以下面的方式实现了
@GetMapping("/export/downloadFile")
public ResponseEntity<byte[]> downloadData(Model model,
@ModelAttribute(EXCEPTION_COLLECTOR) ExceptionCollector exceptionCollector,
@RequestParam("userName") String userName,
@RequestParam("startDate") Date startDate,
@RequestParam("endDate") Date endDate,
@RequestParam("reason") String reason) {
URI uri = /building url here/;
return restTemplate.exchange(uri, HttpMethod.GET, new HttpEntity<>(httpHeaders), byte[].class);
}
因为我使用的是 ResponseEntity<byte[]> ,所以休息模板会等到整个文件加载到内存中。所以我经常遇到套接字超时问题。
我们是否有办法将响应作为流读取并返回到控制器。
我发现一些关于 restTemplate.execute 的事情。
restTemplate.execute(uri,HttpMethod.GET,requestCallBack,clientHttpResponse -> {
File ret = File.createTempFile("download", ".zip",new File("/Users/bokkavijay/Desktop"));
StreamUtils.copy(clientHttpResponse.getBody(), new FileOutputStream(ret));
return ret;
});
上面的代码片段可以将文件复制到我们的本地而不会超时,但这不是我需要的。
我们如何将 clientHttpResponse 中的流通过管道传输到控制器?