本文主要是介绍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();
}
}
这篇关于httpClient通过FileUtils工具下载的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!