刘华
实验室:JS测试器
分类专栏
hadoop2篇
mysql1篇
nginx8篇
java6篇
架构设计3篇
信息安全6篇
前端开发2篇
数据同步6篇
AI7篇
其他9篇

基于hutool下载文件的方法

liuhua-2024/4/30 18:35:08
/*基于hutool下载文件的方法*/
public void downloadFile(String url,File file) throws IOException{
    HttpRequest request=HttpRequest.get(url);
    request.timeout(60000);
    HttpResponse response=request.execute();
    InputStream in = response.bodyStream();
    try (FileOutputStream out=new FileOutputStream(file)) {
        byte[] buffer=new byte[4096];
        int len;
        while ((len=in.read(buffer))!=-1){
            out.write(buffer,0,len);
        }
        out.flush();
    }
    catch (Exception e){
        log.info("通过自编写方法下载失败");
        throw new IOException("下载失败",e);
    }
    finally {
        response.close();
        try {
            in.close();
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}


小钉 京ICP备16032583号-1