码迷,mamicode.com
首页 > 编程语言 > 详细

java下载文件写的工具类

时间:2020-06-10 11:25:42      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:exce   tst   工具类   pat   path   delete   int   NPU   cep   

提供一个文件的网址

 1 ...
 2     private File getNetUrlHttp(String netUrl, String filePath) throws IOException {
 3         File file = null;
 4         URL urlfile;
 5         InputStream inStream = null;
 6         OutputStream os = null;
 7         file = new File(filePath);
 8         if (file.exists()) {
 9             file.delete();
10         }
11         file.createNewFile();
12         //下载网址
13         urlfile = new URL(netUrl);
14         inStream = urlfile.openStream();
15         os = new FileOutputStream(file);
16         int bytesRead = 0;
17         byte[] buffer = new byte[8192];
18         while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
19             os.write(buffer, 0, bytesRead);
20         }
21         if (null != os) {
22             os.close();
23         }
24         if (null != inStream) {
25             inStream.close();
26         }
27         return file;
28     }
29 ...

 

java下载文件写的工具类

标签:exce   tst   工具类   pat   path   delete   int   NPU   cep   

原文地址:https://www.cnblogs.com/lgqrlchinese/p/13083408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!