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();
        }
    }

猜你喜欢

转载自blog.csdn.net/u013089490/article/details/83508290