使用Spring处理StreamingResponseBody

huangapple go评论135阅读模式
英文:

Consuming a StreamingResponseBody with Spring

问题

我有一个简单的 Web 服务,使用 StreamingResponseBody 流式传输文件。定义如下:

  1. @GetMapping("/files/{filename}")
  2. public ResponseEntity<StreamingResponseBody> download(@PathVariable String filename) {
  3. ...
  4. StreamingResponseBody responseBody = out -> {
  5. ...
  6. };
  7. HttpHeaders httpHeaders = new HttpHeaders();
  8. httpHeaders.setContentLength(byteArray.length);
  9. return new ResponseEntity(responseBody, httpHeaders, HttpStatus.OK);
  10. }

它工作得很好,但现在,我需要在客户端应用程序中使用它。我正在使用 Spring 进行消费,但我找不到一种在流传输的同时将其读取并写入文件的方法...

我尝试使用 Feign,但似乎不支持它。
我尝试使用 RestTemplate,但无法使其工作...

Spring 是否支持客户端流式传输?
有人知道如何做到这一点吗?
也许可以使用纯 Java API?

非常感谢您的帮助!

英文:

I've got a simple web-service that stream a file using a StreamingResponseBody.
The definition looks like this:

  1. @GetMapping(&quot;/files/{filename}&quot;)
  2. public ResponseEntity&lt;StreamingResponseBody&gt; download(@PathVariable String filename) {
  3. ...
  4. StreamingResponseBody responseBody = out -&gt; {
  5. ...
  6. }
  7. HttpHeaders httpHeaders = new HttpHeaders();
  8. httpHeaders.setContentLength(byteArray.length);
  9. return new ResponseEntity(responseBody, httpHeaders, HttpStatus.OK);
  10. }

It works well, but now, I need to consume it in a client application.
I'm using spring to consume it, but I can't find a way to read the stream and write it to a file as it flows...

I tryied using feign but it seems it doesn't support it.
I tryied using restTemplate but I can't make it work...

Does spring support streaming client side ?
Does anybody know how to do this ?
Perhaps using pure java API ?

Thanks a lot for your help !

答案1

得分: 1

你可以使用Apache Http Client(org.apache.httpcomponents:httpclient:4.5.12):

  1. URI uri = new URIBuilder()
  2. .setScheme(scheme)
  3. .setHost(host)
  4. .setPort(port)
  5. .setPath(url)
  6. .build();
  7. HttpUriRequest request = RequestBuilder.get(uri).build();
  8. try (CloseableHttpClient httpClient = HttpClients.createDefault();
  9. CloseableHttpResponse httpResponse = httpClient.execute(request);
  10. InputStream inputStream = httpResponse.getEntity().getContent()) {
  11. // 随意处理流,例如可以使用FileOutputStream将其写入文件,使用上述的 'inputStream'。
  12. }
英文:

You can use Apache Http Client (org.apache.httpcomponents:httpclient:4.5.12):

  1. URI uri = new URIBuilder()
  2. .setScheme(scheme)
  3. .setHost(host)
  4. .setPort(port)
  5. .setPath(url)
  6. .build();
  7. HttpUriRequest request = RequestBuilder.get(uri).build();
  8. try (CloseableHttpClient httpClient = HttpClients.createDefault();
  9. CloseableHttpResponse httpResponse = httpClient.execute(request);
  10. InputStream inputStream = httpResponse.getEntity().getContent()) {
  11. // Do with stream whatever you want, for example put it to File using FileOutputStream and &#39;inputStream&#39; above.
  12. }

huangapple
  • 本文由 发表于 2020年8月31日 08:06:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63663259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定