-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiThreadDownload.java
More file actions
173 lines (150 loc) · 6.33 KB
/
MultiThreadDownload.java
File metadata and controls
173 lines (150 loc) · 6.33 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package MultiThreadDownload;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
/**
* Created by zhenweiyu on 2017/5/11.
*/
public class MultiThreadDownload {
private CountDownLatch countDownLatch = null;
public void download(String url,int threadNum){
countDownLatch = new CountDownLatch(threadNum);
try {
URL connectUrl = new URL(url);
try {
URLConnection urlConnection = connectUrl.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setRequestProperty("Accept-Encoding","identity");
httpURLConnection.setDoInput(true);
httpURLConnection.setConnectTimeout(5000);
printHttpHeader(httpURLConnection);
if(httpURLConnection.getResponseCode()==200){
int fileLength = httpURLConnection.getContentLength();
File file = new File(getFileName(url));
RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rwd");
randomAccessFile.setLength(fileLength);
randomAccessFile.close();
int part = fileLength/threadNum;
int remainPart = fileLength%threadNum;//the last thread download this part;
int start = 0;
int end = part-1;
for(int threadIndex = 0;threadIndex<threadNum;threadIndex++){
Thread downloadThread = new DownloadThread(url,file,start,end);
downloadThread.start();
start = end+1;
if(threadIndex!=threadNum-1){
end += part;
}
else {
end += part+remainPart;
}
}
new Thread(new Runnable() {
@Override
public void run() {
try {
System.out.println("downloading...");
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
// if(checkFileCompleted(file,fileLength)){
// System.out.println("download successfully!");
// }else {
// System.out.println("download failure");
// }
System.out.println("download successfully!");
}
}).start();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
class DownloadThread extends Thread{
private URL url;
private File file;
private int start;
private int end;
public DownloadThread(String urlStr,File file,int start,int end){
try {
url = new URL(urlStr);
} catch (MalformedURLException e) {
e.printStackTrace();
}
this.file = file;
this.start = start;
this.end = end;
}
@Override
public void run() {
try {
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection)urlConnection;
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Rang",String.format("bytes=%d-%d",start,end));
RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rwd");
randomAccessFile.seek(start);
if(httpURLConnection.getResponseCode()==200){
InputStream inputStream = httpURLConnection.getInputStream();
byte []buffer = new byte[1024];
int len = 0;
while ((len = inputStream.read(buffer))!=-1){
randomAccessFile.write(buffer,0,len);
}
randomAccessFile.close();
inputStream.close();
}else{
System.out.println(Thread.currentThread().getName()+"download fail");
}
} catch (IOException e) {
e.printStackTrace();
}finally {
countDownLatch.countDown();
}
}
}
private String getFileName(String url){
return url.substring(url.lastIndexOf("/")+1);
}
private void printHttpHeader(HttpURLConnection connection){
Map<String, List<String>> headerFields = connection.getHeaderFields();
Set<Map.Entry<String, List<String>>> entrySet = headerFields.entrySet();
Iterator<Map.Entry<String, List<String>>> iterator = entrySet.iterator();
while(iterator.hasNext()) {
Map.Entry<String, List<String>> next = iterator.next();
String key=next.getKey();
List<String> value = next.getValue();
if(key==null)
System.out.println(value.toString());
else
System.out.println(key+":"+value.toString());
}
}
// private boolean checkFileCompleted(File file,int fileLength){
//// System.out.println(file.length()+" "+fileLength);
//// if(file.length()==fileLength){
//// return true;
//// }return false;
// return false;
// }
public static void main(String []args){
MultiThreadDownload multiThreadDownload = new MultiThreadDownload();
multiThreadDownload.download("http://dl.softmgr.qq.com/original/desktop/sogou_wallpaper_2.5j-2.5.4.2687.exe",4);
}
}