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
package com.changfa.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class DownloadUtil {
private String pathFile;
private String strFile;
private DownloadThread[] downloadThreadArr;
private int threadNum;
private int size;
public DownloadUtil (String pathFile, String strFile, int threadNum) {
this.pathFile = pathFile;
this.threadNum = threadNum;
downloadThreadArr = new DownloadThread[threadNum];
this.strFile = strFile;
}
public void download() throws Exception {
URL url = new URL(pathFile);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
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");
size = conn.getContentLength();
conn.disconnect();
int currentPartSize = size / threadNum + 1;
RandomAccessFile file = new RandomAccessFile(strFile, "rw");
file.setLength(size);
file.close();
for (int i = 0; i < threadNum; i++) {
int start = i * currentPartSize;
RandomAccessFile currentPart = new RandomAccessFile(strFile, "rw");
currentPart.seek(start);
downloadThreadArr[i] = new DownloadThread(start, currentPartSize, currentPart);
downloadThreadArr[i].start();
}
}
class DownloadThread extends Thread {
private int start;
private int size;
private RandomAccessFile out;
public int length;
public DownloadThread(int start, int size,RandomAccessFile out) {
this.out = out;
this.start = start;
this.size = size;
}
@Override
public void run() {
InputStream is = null;
try {
URL url = new URL(pathFile);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);
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");
is = conn.getInputStream();
is.skip(this.start);
byte[] bs = new byte[1024];
int len = 0;
while ((len = is.read(bs)) != -1 && length < size) {
out.write(bs, 0, len);
length += len;
}
out.close();
is.close();
}
catch (Exception e) {
e.printStackTrace();
}finally {
try {
out.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
/*
@RestController
@RequestMapping("tc")
public class TestController {
@RequestMapping("/download")
public void download(String filePath,String savePath) {
try {
DownloadUtil downloadUtil = new DownloadUtil (filePath,savePath,10);
downloadUtil.download();
}catch (Exception e){
e.printStackTrace();
}
}
}*/