httpClient通過FileUtils工具下載
httpClient通過FileUtils工具下載,而FileUtils工具是common-io包的工具。
private static final Logger LOGGER = LoggerFactory.getLogger(HttpService.class);
@Autowired
private CloseableHttpClient httpClient;
@Autowired
private RequestConfig requestConfig;
/**
* 下載檔案
* @param url 檔案url
* @param dest 目標目錄
* @throws Exception
*/
public void downloadFile(String url, File dest) throws Exception {
LOGGER.info("下載檔案,URL:" + url);
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
FileUtils.writeByteArrayToFile(dest, IOUtils.toByteArray(response.getEntity().getContent()));
} finally {
response.close();
}
}