JAVA从网络下载文件到本地

转载自https://blog.csdn.net/vison155142/article/details/76037647


1.首推一种方法,封装好的FileUtils,简单一句话就搞定

[java]  view plain  copy
  1. /** 
  2.      * 下载文件---返回下载后的文件存储路径 
  3.      *  
  4.      * @param url 文件地址 
  5.      * @param dir 存储目录 
  6.      * @param fileName 存储文件名 
  7.      * @return 
  8.      */  
  9.     public static void downloadHttpUrl(String url, String dir, String fileName) {  
  10.         try {  
  11.             URL httpurl = new URL(url);  
  12.             File dirfile = new File(dir);    
  13.             if (!dirfile.exists()) {    
  14.                 dirfile.mkdirs();  
  15.             }  
  16.             FileUtils.copyURLToFile(httpurl, new File(dir+fileName));  
  17.         } catch (Exception e) {  
  18.             e.printStackTrace();  
  19.         }  
  20.     }  

2.第二种方法,常规写法,不多说

[java]  view plain  copy
  1. /** 
  2.  * 从网络Url中下载文件 
  3.  *  
  4.  * @param urlStr 
  5.  * @param fileName 
  6.  * @param savePath 
  7.  * @throws IOException 
  8.  */  
  9. public static String downLoadFromUrl(String urlStr, String fileName, String savePath) {  
  10.     try {  
  11.   
  12.         URL url = new URL(urlStr);  
  13.         HttpURLConnection conn = (HttpURLConnection) url.openConnection();  
  14.         // 设置超时间为3秒  
  15.         conn.setConnectTimeout(3 * 1000);  
  16.         // 防止屏蔽程序抓取而返回403错误  
  17.         conn.setRequestProperty("User-Agent""Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");  
  18.   
  19.         // 得到输入流  
  20.         InputStream inputStream = conn.getInputStream();  
  21.         // 获取自己数组  
  22.         byte[] getData = readInputStream(inputStream);  
  23.   
  24.         // 文件保存位置  
  25.         File saveDir = new File(savePath);  
  26.         if (!saveDir.exists()) {  
  27.             saveDir.mkdir();  
  28.         }  
  29.         File file = new File(saveDir + File.separator + fileName);  
  30.         FileOutputStream fos = new FileOutputStream(file);  
  31.         fos.write(getData);  
  32.         if (fos != null) {  
  33.             fos.close();  
  34.         }  
  35.         if (inputStream != null) {  
  36.             inputStream.close();  
  37.         }  
  38.         // System.out.println("info:"+url+" download success");  
  39.         return saveDir + File.separator + fileName;  
  40.     } catch (Exception e) {  
  41.         e.printStackTrace();  
  42.     }  
  43.     return "";  
  44.   
  45. }  
  46.   
  47. /** 
  48.  * 从输入流中获取字节数组 
  49.  *  
  50.  * @param inputStream 
  51.  * @return 
  52.  * @throws IOException 
  53.  */  
  54. public static byte[] readInputStream(InputStream inputStream) throws IOException {  
  55.     byte[] buffer = new byte[1024];  
  56.     int len = 0;  
  57.     ByteArrayOutputStream bos = new ByteArrayOutputStream();  
  58.     while ((len = inputStream.read(buffer)) != -1) {  
  59.         bos.write(buffer, 0, len);  
  60.     }  
  61.     bos.close();  
  62.     return bos.toByteArray();  
  63. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值