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