-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDownUtil.java
103 lines (93 loc) · 3.18 KB
/
DownUtil.java
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
import java.io.*;
import java.net.*;
public class DownUtil{
private String path;
private String targetFile;
private int threadNum;
private DownThread[] threads;
private int fileSize;
public DownUtil(String path, String targetFile, int threadNum){
this.path = path;
this.targetFile = targetFile;
this.threadNum = threadNum;
threads = new DownThread[threadNum];
}
public void download() throws Exception{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
conn.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
+ "application/x-shockwave-flash, application/xaml+xml, "
+ "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
+ "application/x-ms-application, application/vnd.ms-excel, "
+ "application/vnd.ms-powerpoint, application/msword, */*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
fileSize = conn.getContentLength();
conn.disconnect();
int currentPartSize = fileSize / threadNum + 1;
RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
file.setLength(fileSize);
file.close();
for(int i=0; i<threadNum; i++){
int startPos = i*currentPartSize;
RandomAccessFile currentPart = new RandomAccessFile(targetFile, "rw");
currentPart.seek(startPos);
threads[i] = new DownThread(startPos, currentPartSize, currentPart);
threads[i].start();
}
}
public double getCompleteRate(){
int sumSize = 0;
for(int i=0; i<threadNum; i++){
sumSize += threads[i].length;
}
return sumSize*1.0/fileSize;
}
private class DownThread extends Thread{
private int startPos;
private int currentPartSize;
private RandomAccessFile currentPart;
public int length;
public DownThread(int startPos, int currentPartSize, RandomAccessFile currentPart){
this.startPos = startPos;
this.currentPartSize = currentPartSize;
this.currentPart = currentPart;
}
public void run(){
try{
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5*1000);
conn.setRequestMethod("GET");
conn.setRequestProperty(
"Accept",
"image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
+ "application/x-shockwave-flash, application/xaml+xml, "
+ "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
+ "application/x-ms-application, application/vnd.ms-excel, "
+ "application/vnd.ms-powerpoint, application/msword, */*");
conn.setRequestProperty("Accept-Language", "zh-CN");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Connection", "Keep-Alive");
InputStream inStream = conn.getInputStream();
byte[] buffer = new byte[1024];
inStream.skip(this.startPos);
int hasRead = 0;
while(length < currentPartSize
&& (hasRead = inStream.read(buffer)) != -1){
currentPart.write(buffer, 0, hasRead);
length += hasRead;
}
currentPart.close( );
inStream.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
}