admin 管理员组文章数量: 1184232
引入的是这个包
import org.springframework.web.client.RestTemplate;
// 通过接口,下载附件,转换成byte数组;然后再把这些字节流传递给XX系统的接口
public void download3(String token) throws IOException {
// 创建 RestTemplate
RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("Authorization","Bearer "+token);
// 调用下载接口进行下载
// id 为String类型,为被调用接口参数
ResponseEntity<byte[]> entity = restTemplate.exchange("http://localhost:8080/-app-/api/flow-mobile/v1/task-form-process/download-attachments/82", HttpMethod.GET,new HttpEntity<>(headers), byte[].class);
// 返回数据,在下面写到输出流里面(由于我是需要把字节数组传递给另外一个系统,所以我注释了下面的代码,我不需要存本地)
byte[] body = entity.getBody();
// 这三行是对文件名编码,与内容编码,可以不写
/* fileName = FileNameUtils.fileNameEncoding(request, fileName);
response.setContentType("application/x-msdownload");
response.addHeader("Content-Disposition", "attachment; " + fileName);*/
// 把boby数据写入输出流输出
//response.getOutputStream().write(body);
// 存到本地
/*if(body != null) {
String filepath = "/Users/xiaolouyijiuzai/Desktop/aa.docx";
File file = new File(filepath);
if (file.exists()) {
file.delete();
}
FileOutputStream fos = new FileOutputStream(file);
fos.write(body, 0, body.length);
fos.flush();
fos.close();
}*/
// 传递给XX系统的接口
this.uploadMaterial("23",body);
}
本文标签: 接口 文件 RestTemplate
版权声明:本文标题:使用restTemplate请求get接口下载文件 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1765407709a3377802.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论