可以使用Spring Boot的RestTemplate下载文件,方法如下:
- 创建一个RestTemplate实例:
@Autowired
private RestTemplate restTemplate;
- 使用exchange方法发送HTTP请求,将响应内容写入文件:
ResponseEntity<byte[]> response = restTemplate.exchange(url, HttpMethod.GET, null, byte[].class);
byte[] file = response.getBody();
try (FileOutputStream fos = new FileOutputStream("path/to/file")) {
fos.write(file);
}
注意: 在使用exchange方法时,需要指定HTTP方法(如GET, POST等)以及响应内容的类型(byte[]类型).
希望这些信息能帮到你!