java下载文件写的工具类

netUrl:提供一个文件的网址

filePath:本地保存的路径

 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 ...
昔日我曾苍老,如今风华正茂(ง •̀_•́)ง
原文地址:https://www.cnblogs.com/lgqrlchinese/p/13083408.html