java - springboot RestTemplate 调用 url 和 https 响应头(内容类型:audio/wav),如何保存为 *.wav 文件?
问题描述
我使用spring RestTemplate 调用第三方服务,</p>
ResponseEntity<String> forEntity = restTemplate.getForEntity(url, String.class);
结果如下:
forEntity status:200 headers: content-type=audio/wav body:"RIFFä,,,xxxxxxx......"
响应是在此处输入图像描述 主体似乎是 wav 数据,我想将数据保存到 wav 文件。
如果我直接去chrome中的链接,就可以玩了,下载。
解决方案
改为使用RestTemplate.execute
,它允许您附加 a ResponseExtractor
,您可以在其中访问response body
which an InputStream
,我们将其InputStream
写入文件
restTemplate.execute(
url,
HttpMethod.GET,
request -> {},
response -> {
//get response body as inputstream
InputStream in = response.getBody();
//write inputstream to a local file
Files.copy(in, Paths.get("C:/path/to/file.wav"), StandardCopyOption.REPLACE_EXISTING);
return null;
}
);
推荐阅读
- arrays - Ruby如何打印/放置读取的文件内容而不是一堆数字/符号
- prometheus - 如何在 prometheus 中存储每个作业或目标具有不同保留时间的数据?
- angular2-routing - 如何在 Angular 2 中将 app-routing.module.ts 拆分为多个文件?
- moodle - 在 Moodle 的左侧导航菜单中添加本地插件
- asp.net-web-api - 限制对所有 ASP.NET Web API 端点的访问,特定范围内的少数除外
- populate - 选择给定日期和标识符的最新值
- amazon-web-services - AWS Data Wrangler2.12.1 中的 wr.redshift.to_sql 失败
- android - 错误:您试图通过调用 firebase.app() 来使用未安装在您的 Android 项目上的 firebase 模块。反应原生
- node.js - How to update all the data that have same field without modify other data in mongoose?
- javascript - Handlebars.js 中花括号内的变量名