-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDownUtil.java
More file actions
152 lines (135 loc) · 5.69 KB
/
Copy pathDownUtil.java
File metadata and controls
152 lines (135 loc) · 5.69 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
package NetworkProgramming;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownUtil {
//顶一下载资源的路径
private String path;
//指定下载资源的文件保存位置
private String targetFile;
//定义下载资源的线程数量
private int threadNum;
//定义下载文件的总大小
private int fileSize;
//定义下载的Thread对象
private DownThread[] threads;
public DownUtil(String path, String targetFile, int threadNum) {
this.path = path;
this.targetFile = targetFile;
this.threadNum = threadNum;
//初始化Threads数组
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/jepg,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进行下载
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.currentPart = currentPart;
this.currentPartSize = currentPartSize;
}
@Override
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/jepg,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");
InputStream inputStream = conn.getInputStream();
//跳过startPos个字节,表明该线程只下载自己负责的部分文件
inputStream.skip(startPos);
byte[] buffer = new byte[1024];
int hasRead = 0;
//读取网络文件
while (length < currentPartSize && (hasRead = inputStream.read(buffer)) >0) {
currentPart.write(buffer, 0, hasRead);
length += hasRead;
}
currentPart.close();
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
//初始化对象
final DownUtil downUtil = new DownUtil("https://i2.hdslb.com/bfs/archive/7aaabf9a278e7147c928bfad79c64b8d5628447c.jpg@160w_100h.jpg",
"test.png", 4);
downUtil.download();
new Thread(
() -> {
while (downUtil.getCompleteRate() < 1) {
//每隔0.1s查询一次任务完成进度
//GUI程序中可根据改进度绘制进度条
System.out.println("已完成:" + downUtil.getCompleteRate());
try {
Thread.sleep(5);
} catch (Exception e) {
e.printStackTrace();
}
}
}
).start();
}
}