-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemp.txt
29 lines (23 loc) · 1.28 KB
/
temp.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new ByteArrayHttpMessageConverter());
HttpHeaders headers = new HttpHeaders();
//headers.add("Authorization", "Bearer SOME_BEARER_TOKEN"); //if requires some token
headers.setAccept(Arrays.asList(MediaType.APPLICATION_PDF));
HttpEntity<String> entity = new HttpEntity<>(headers);
try {
ResponseEntity<Resource> response = restTemplate.exchange(
"https://SOME_API.COM/api/docs/12345",
HttpMethod.GET, entity, Resource.class);
if (response.getStatusCode() == HttpStatus.OK) {
String disposition = response.getHeaders().get("Content-Disposition").get(0);
String fileName = disposition.replaceFirst("(?i)^.*filename=\"?([^\"]+)\"?.*$", "$1");//get the filename from the Content-Disposition header
fileName = URLDecoder.decode(fileName, String.valueOf(StandardCharsets.ISO_8859_1));
//save to examine file
File targetFile = new File("c:/temp/" + fileName);
FileUtils.copyInputStreamToFile(response.getBody().getInputStream(), targetFile);
}
} catch (RestClientException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}