这是我的方法。根据春季Jira问题的提示。
RestTemplate restTemplate // = ...;// Optional Accept headerRequestCallback requestCallback = request -> request.getHeaders() .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL));// Streams the response instead of loading it all in memoryResponseExtractor<Void> responseExtractor = response -> { // Here I write the response to a file but do what you like Path path = Paths.get("some/path"); Files.copy(response.getBody(), path); return null;};restTemplate.execute(URI.create("www.something.com"), HttpMethod.GET, requestCallback, responseExtractor);从前面提到的吉拉问题:
请注意,您不能简单地从提取器返回InputStream,因为到execute方法返回时,基础连接和流已经关闭。
春季5更新
Spring 5引入了
WebClient允许异步(例如,非阻塞)http请求的类。从文档中:
与RestTemplate相比,WebClient是:
- 非阻塞,响应式,并以更少的硬件资源支持更高的并发性。
- 提供了利用Java 8 lambda的功能性API。
- 支持同步和异步方案。
- 支持从服务器上流或下流。



