使用restTemplate请求get接口下载文件

引入的是这个包
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);
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值