栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用REST模板Java Spring MVC从服务器下载大文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用REST模板Java Spring MVC从服务器下载大文件

这是我的方法。基于springJira问题的提示。

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方法返回时,基础连接和流已经关闭。

Update for Spring 5

Spring 5引入了WebClient允许异步(例如,非阻塞)http请求的类。从文档:

与RestTemplate相比,WebClient是:

  • non-blocking, reactive, and supports higher concurrency with less hardware resources.
  • provides a functional API that takes advantage of Java 8 lambdas.
  • supports both synchronous and asynchronous scenarios.
  • supports streaming up or down from a server.


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/365390.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号