欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件下载工具类 传入url

程序员文章站 2022-04-28 12:45:39
...

记录文件工具类 根据get 请求方式下载文件

public class FileDownload {
 
 /**
  * 文件下载
  * @param url 链接地址
  * @param path 要保存的路径及文件名
  * @return
  */
 public static boolean download(String url,String path){
   
   boolean flag = false;
   
   CloseableHttpClient httpclient = HttpClients.createDefault();
   RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(2000)
       .setConnectTimeout(2000).build();

   HttpGet get = new HttpGet(url);
   get.setConfig(requestConfig);
   
   BufferedInputStream in = null;
   BufferedOutputStream out = null;
   try{
     for(int i=0;i<3;i++){
       CloseableHttpResponse result = httpclient.execute(get);
       System.out.println(result.getStatusLine());
       if(result.getStatusLine().getStatusCode() == 200){
         in = new BufferedInputStream(result.getEntity().getContent());
         File file = new File(path);
         out = new BufferedOutputStream(new FileOutputStream(file));
         byte[] buffer = new byte[1024];
         int len = -1;
         while((len = in.read(buffer,0,1024)) > -1){
           out.write(buffer,0,len);
         }
         flag = true;
         break;
       }else if(result.getStatusLine().getStatusCode() == 500){
         continue ;
       }
     }
     
   }catch(Exception e){
     e.printStackTrace();
     flag = false;
   }finally{
     get.releaseConnection();
     try{
       if(in != null){
         in.close();
       }
       if(out != null){
         out.close();
       }
     }catch(Exception e){
       e.printStackTrace();
       flag = false;
     }
   }
   return flag;
 }

 private static Log log = LogFactory.getLog(FileDownload.class);
}

 

相关标签: file