参考网络代码修改
import java.io.File;
import java.io.InputStream;import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MultiDowloadThread {
public static void main(String[] args) {
String url = "http://127.0.0.1:7001/ext4api.zip";
String filePath = "D:\\1.txt";
new DownloadFile(5, url, filePath).download();
}
}
class DownloadFile {
private File localFile;
private String url;
private long fileSize;
private int threadCount;
private String filePath;
public DownloadFile(int count,String url,String filePath){
this.threadCount = count;
this.url = url;
this.filePath = filePath;
}
public void download(){
this.createFileByUrl();
long block = this.fileSize % this.threadCount == 0 ? this.fileSize / this.threadCount : this.fileSize / this.threadCount + 1;
for(int i=0;i<this.threadCount;i++){
long start = i * block;
long end = start + block >= this.fileSize ? this.fileSize : start + block - 1;
new DownloadFileThread(this.url, this.localFile, start, end).start();
}
}
private void createFileByUrl() {
try {
this.localFile = new File(this.filePath);
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(15 * 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("Referer", url);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.connect();
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
RandomAccessFile raf = new RandomAccessFile(this.localFile,
"rw");
this.fileSize = conn.getContentLength();
System.err.println("fileSize " + fileSize);
raf.setLength(fileSize);
raf.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
class DownloadFileThread extends Thread {
private String url;
private File localFile;
private long start;
private long end;
public DownloadFileThread(String url, File localFile, long start, long end){
this.url = url;
this.localFile = localFile;
this.start = start;
this.end = end;
}
@Override
public void run() {
try {
URL u = new URL(url);
HttpURLConnection conn = (HttpURLConnection) u.openConnection();
conn.setConnectTimeout(15 * 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("Referer", url);
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty(
"User-Agent",
"Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.2; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("Range", "bytes=" + start + "-" + end);// 设置获取实体数据的范围
conn.connect();
System.err.println(conn.getResponseCode());
/**
* 代表服务器已经成功处理了部分GET请求
*/
if (conn.getResponseCode() == 206) {
InputStream is = conn.getInputStream();
int len = 0;
byte[] buf = new byte[1024];
RandomAccessFile raf = new RandomAccessFile(localFile, "rwd");
raf.seek(start);
while ((len = is.read(buf)) != -1) {
raf.write(buf, 0, len);
}
raf.close();
is.close();
System.out.println(Thread.currentThread().getName() + "完成下载 : " + start + " -- " + end);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}