spring-boot - 用springboot resttemplate下载一个文件
问题描述
我使用此代码下载文件:
HttpHeaders httpHeaders = recuperaHeaders();
File file = restTemplate.execute(url, HttpMethod.GET, null, clientHttpResponse -> {
File ret = File.createTempFile("download", ".PDF");
InputStream body = clientHttpResponse.getBody();
StreamUtils.copy(body, new FileOutputStream(ret));
return ret;
});
return file;
但是调用返回 302(重定向)的 URL 总是说当我使用 GET 时重定向是自动的,但是文件总是返回大小为 0,如果我将 URL 粘贴到浏览器中工作正常
这是我的日志:
2021-08-11 16:40:54.217 DEBUG 2149 --- [ restartedMain] o.s.web.client.RestTemplate : HTTP GET <<Myurl.....>>
2021-08-11 16:40:54.697 DEBUG 2149 --- [ restartedMain] o.s.web.client.RestTemplate : Response 302 FOUND
2021-08-11 16:59:14.609 DEBUG 2149 --- [ restartedMain] b.c.l.b.api.BalcaoonlineApiApplication : File Size: 0
如果我使用这样的公共文件(https://www.mds.gov.br/webarquivos/public/NOBSUAS_2012.pdf)在谷歌中找到,下载工作正常..我使用来自 Baeldung 网站的示例:https ://www.baeldung.com/spring-resttemplate-download-large-file#download-without-resume
解决方案
推荐阅读
- python - 如何将 Python GUI 的整个文件夹转换为单个可执行文件?
- azure-ad-b2c - 如何使用从内置策略和自定义策略生成的令牌?
- python - 如何从字符串变量中定位类的特定实例
- php - 下载 PHP 而不是运行
- android - 新的
- python - Pandas 循环中的多个正则表达式替换
- cytoscape.js - cystoscape 中的顺序边缘动画
- mysql - 这个查询慢吗
- javascript - render_template 不呈现 POST 请求中指定的模板
- mod-auth-openidc - 取消保护特定
当整个站点受到 mod-auth-openidc 保护时