废话不多说了,直接给大家贴代码了,具体代码如下所示;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import org.apache.commons.io.FileUtils; /** * 文件工具类 * @author panda fang * @date 2017-08-26 * @version 1.0 */ public class FileTool { /** * 使用传统io stream 下载文件 * @param url * @param saveDir * @param fileName */ public static void download(String url, String saveDir, String fileName) { BufferedOutputStream bos = null ; InputStream is = null ; try { byte [] buff = new byte [ 8192 ]; is = new URL(url).openStream(); File file = new File(saveDir, fileName); file.getParentFile().mkdirs(); bos = new BufferedOutputStream( new FileOutputStream(file)); int count = 0 ; while ( (count = is.read(buff)) != - 1 ) { bos.write(buff, 0 , count); } } catch (IOException e) { e.printStackTrace(); } finally { if (is != null ) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null ) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 利用 commonio 库下载文件,依赖Apache Common IO ,官网 https://commons.apache.org/proper/commons-io/ * @param url * @param saveDir * @param fileName */ public static void downloadByApacheCommonIO(String url, String saveDir, String fileName) { try { FileUtils.copyURLToFile( new URL(url), new File(saveDir, fileName)); } catch (IOException e) { e.printStackTrace(); } } /** * 使用NIO下载文件, 需要 jdk 1.4+ * @param url * @param saveDir * @param fileName */ public static void downloadByNIO(String url, String saveDir, String fileName) { ReadableByteChannel rbc = null ; FileOutputStream fos = null ; FileChannel foutc = null ; try { rbc = Channels.newChannel( new URL(url).openStream()); File file = new File(saveDir, fileName); file.getParentFile().mkdirs(); fos = new FileOutputStream(file); foutc = fos.getChannel(); foutc.transferFrom(rbc, 0 , Long.MAX_VALUE); } catch (IOException e) { e.printStackTrace(); } finally { if (rbc != null ) { try { rbc.close(); } catch (IOException e) { e.printStackTrace(); } } if (foutc != null ) { try { foutc.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 使用NIO下载文件, 需要 jdk 1.7+ * @param url * @param saveDir * @param fileName */ public static void downloadByNIO2(String url, String saveDir, String fileName) { try (InputStream ins = new URL(url).openStream()) { Path target = Paths.get(saveDir, fileName); Files.createDirectories(target.getParent()); Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } } } |
下载一个百度logo 测试一下
1
2
3
4
|
public static void main(String[] args) { FileTool.downloadByNIO2( "http://www.baidu.com/img/bd_logo1.png" , "/home/panda/picture" , "baidu_logo.png" ); System.out.println( "done..." ); } |
总结
以上所述是小编给大家介绍的Java 从网上下载文件的几种方式实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!