首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Spring中使用StreamingResponseBody

在Spring中使用StreamingResponseBody
EN

Stack Overflow用户
提问于 2020-08-31 08:06:09
回答 1查看 668关注 0票数 0

我有一个简单的web服务,可以使用StreamingResponseBody流式传输文件。其定义如下所示:

代码语言:javascript
复制
@GetMapping("/files/{filename}")
public ResponseEntity<StreamingResponseBody> download(@PathVariable String filename) {
    ...
    StreamingResponseBody responseBody = out -> {
        ...
    }
    HttpHeaders httpHeaders = new HttpHeaders();
    httpHeaders.setContentLength(byteArray.length);

    return new ResponseEntity(responseBody, httpHeaders, HttpStatus.OK);
}

它工作得很好,但是现在,我需要在客户端应用程序中使用它。我正在使用spring来使用它,但是我找不到一种方法来读取流,并在它流动时将其写入到文件中……

我试过使用feign,但似乎不支持它。我试着使用restTemplate,但我不能让它工作...

spring是否支持流媒体客户端?有人知道怎么做吗?也许使用纯java API?

非常感谢你的帮助!

EN

回答 1

Stack Overflow用户

发布于 2021-04-06 18:32:49

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

代码语言:javascript
复制
URI uri = new URIBuilder()
        .setScheme(scheme)
        .setHost(host)
        .setPort(port)
        .setPath(url)
        .build();
HttpUriRequest request = RequestBuilder.get(uri).build();

try (CloseableHttpClient httpClient = HttpClients.createDefault();
     CloseableHttpResponse httpResponse = httpClient.execute(request);
     InputStream inputStream = httpResponse.getEntity().getContent()) {

    // Do with stream whatever you want, for example put it to File using FileOutputStream and 'inputStream' above.

}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63663259

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档