forked from nibnait/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownloadUtil.java
More file actions
144 lines (128 loc) · 4.56 KB
/
DownloadUtil.java
File metadata and controls
144 lines (128 loc) · 4.56 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package common.util;
import com.google.common.collect.Lists;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.List;
/**
* Created by nibnait on 2022/01/09
*/
public class DownloadUtil {
public static void main(String[] args) throws InterruptedException {
downloadA("http://tb.nsfocus.co/1R2A8952.jpg", "/Users/nibnait/Downloads/qiniuyun/1R2A8952.jpg");
downloadB("http://tb.nsfocus.co/1R2A8961.jpg", "1R2A8961.jpg", "/Users/nibnait/Downloads/qiniuyun");
List<String> urlList = Lists.newArrayList("http://tb.nsfocus.co/1R2A8962.jpg");
for (String url : urlList) {
String fileName = decode(url.substring(url.lastIndexOf("/")));
downloadB(url, fileName, "/Users/nibnait/Downloads/qiniuyun");
System.out.println(fileName);
Thread.sleep(1000);
}
}
public static String decode(String url) {
try {
String prevURL = "";
String decodeURL = url;
while (!prevURL.equals(decodeURL)) {
prevURL = decodeURL;
decodeURL = URLDecoder.decode(decodeURL, "UTF-8");
}
return decodeURL;
} catch (UnsupportedEncodingException e) {
return "Issue while decoding" + e.getMessage();
}
}
/**
* 根据链接地址下载文件
*
* @param downloadUrl 文件链接地址
* @param path 下载存放文件地址 + 文件名
*/
private static void downloadA(String downloadUrl, String path) {
URL url = null;
DataInputStream dataInputStream = null;
FileOutputStream fileOutputStream = null;
try {
url = new URL(downloadUrl);
dataInputStream = new DataInputStream(url.openStream());
fileOutputStream = new FileOutputStream(new File(path));
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = dataInputStream.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
fileOutputStream.write(output.toByteArray());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @param downloadUrl 文件链接地址
* @param filename 保存文件名
* @param filePath 保存文件路径
*/
private static void downloadB(String downloadUrl, String filename, String filePath) {
URL url = null;
InputStream inputStream = null;
OutputStream outputStream = null;
try {
url = new URL(downloadUrl);
// 打开连接
URLConnection con = url.openConnection();
// 请求超时:5s
con.setConnectTimeout(5 * 1000);
inputStream = con.getInputStream();
byte[] bytes = new byte[1024];
// 读取到的数据长度
int length;
File savePath = new File(filePath);
if (!savePath.exists()) {
// 如果不存在当前文件夹,则创建该文件夹
boolean mkdir = savePath.mkdirs();
if (!mkdir) {
System.out.println("创建文件夹失败");
return;
}
}
outputStream = new FileOutputStream(savePath.getPath() + "/" + filename);
// 读取
while ((length = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, length);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}