【问题标题】:Spring RestTemplate streaming response into another requestSpring RestTemplate 将响应流式传输到另一个请求中
【发布时间】:2017-01-25 15:54:57
【问题描述】:

我正在尝试使用 spring 的 RestTemplate 将文件下载的结果直接流式传输到另一个帖子中

我目前的做法如下:

   ResponseEntity<InputStreamResource> downloadResponse = restTemplate.getForEntity(fileToDownloadUri, InputStreamResource.class);

   InputStreamResource imageInputStreamResource = downloadResponse.getBody();

   ResponseEntity<String> response = restTemplate.exchange(storageUri, POST, new HttpEntity<>(imageInputStreamResource), String.class);

但是,运行上面的代码时出现以下异常:

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "http://host:port/path/some.jpg": stream is closed; nested exception is java.io.IOException: stream is closed

    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:6
...
Caused by: java.io.IOException: stream is closed
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.ensureOpen(HttpURLConnection.java:3348)
    at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3373)

似乎响应总是作为处理的最后一步关闭。随着响应,HttpURLConnection 被关闭,流不再可处理。

我希望能够实现此方案,而不必将文件完全保存在内存中或将其写入文件(如 here 所述)。

非常感谢任何提示。

【问题讨论】:

  • 这里可能是相同的解决方案stackoverflow.com/questions/15781885/…
  • @Redlab 这是一个FileSystemResource - 这会起作用 - 我的问题是InputStream 在GET 请求完成后关闭。所以我不能再往下用了。

标签: java spring spring-mvc


【解决方案1】:

如果您想直接转发响应而不将其保存在内存中,则必须直接写入响应:

@RequestMapping(value = "/yourEndPoint")
public void processRequest(HttpServletResponse response) {
    RestTemplate restTemplate = new RestTemplate();

    response.setStatus(HttpStatus.OK.value());

    restTemplate.execute(
        fileToDownloadUri,
        HttpMethod.GET,
        (ClientHttpRequest requestCallback) -> {},
        responseExtractor -> {
            IOUtils.copy(responseExtractor.getBody(), response.getOutputStream());
            return null;
        });
}

【讨论】:

  • 谢谢你的片段,它的作品。但是我们能以某种方式传递标头吗?
  • 好的,我找到了解决方案:(ClientHttpRequest requestCallback) -> { requestCallback.getHeaders().add(ACCEPT, TEXT_CSV_MEDIA_TYPE_VALUE); }
【解决方案2】:

由于您告诉 RestTemplate 期待 InputStreamResource,它会尝试使用适当的转换器将您的消息转换为 InputStreamResource。 (我猜没有你想要的处理这个)

您应该能够让它期待一个资源,您可以从中获取输入流并读取它。

 import org.springframework.core.io.Resource;

 ResponseEntity<Resource> exchange = RestTemplate.exchange(url, HttpMethod.GET, new HttpEntity(httpHeaders), Resource.class);
 InputStream inputStream = exchange.getBody().getInputStream();

使用它,您可以将响应写入其他地方。 Files.write(inputStream, new File("./test.json")); 为我编写了文件,所以我假设输入流也可以在其他地方使用。 (我用的是 Spring 4.3.5)

编辑:

正如 OP 所述,这仍会将文件加载到内存中。在幕后,InputStream 是一个 ByteArrayInputStream。

默认的 RestTemplate 和 MessageConverters 根本不用于流式传输内容。 您可以编写自己的 org.springframework.web.client.ResponseExtractor 实现,也可以编写 MessageConverter。在 ResponseExtractor 你可以访问org.springframework.http.client.ClientHttpResponse

恕我直言,对于您的用例,您最好使用 Apache Httpcomponents HttpClient 找到HttpEntity#writeTo(OutputStream)。

【讨论】:

  • 恐怕我不明白 - 这正是我正在做的 - ResponseEntity&lt;InputStreamResource&gt; downloadResponse = restTemplate.getForEntity(fileToDownloadUri, InputStreamResource.class); 但在通话后 inputStream 已经关闭。您是否尝试过在上面的代码中使用 InputStream?
  • 你说得对,我把 String 和 InputStreamResource 弄错了(会正确回答)。但是仍然使用 org.springframework.core.io.Resource 而不是 InputStreamResource
  • 感谢您的输入 - 如果我让它返回 Resource 而不是 getForEntity 返回 ByteArrayResource。所以我将完整的文件加载到内存中。这是我想避免的——这就是我尝试InputStreamResource 的原因。我想将响应流式传输到帖子正文中。
  • 你是对的,我认为 RestTemplate 不适合你的用例
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-08
  • 1970-01-01
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
  • 2018-02-19
  • 1970-01-01
相关资源
最近更新 更多