RestTemplate 文件下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*下载签字文件
* inUrl - 请求文件地址:http://blog.hachuizi.com/aaa.docx
* outUrl - 本地缓存文件地址: /home/filePath/aaa.docx
*/

public static String restPdfFile(String inUrl, String outUrl) throws Exception{
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    HttpEntity<Resource> httpEntity = new HttpEntity<Resource>(headers);
    ResponseEntity<byte[]> response = restTemplate.exchange(inUrl, HttpMethod.GET,
            httpEntity, byte[].class);
    System.out.println("状态码"+response.getStatusCodeValue());
    System.out.println("返回信息"+response.getHeaders().getContentType());
    System.out.println("返回信息"+response.getHeaders().getContentType().getSubtype());
    try {
        File file = new File(outUrl);
        FileOutputStream fos = new FileOutputStream(file);
        fos.write(response.getBody());
        fos.flush();
        fos.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return outUrl;
}
ˆ Back To Top