java 多线程分段下载
介绍
使用java 多线程下载,当前只是一个demo,还没进行对比测试,目前看速度确实要快一些
实现和简单就是启用多个现成分段下载,最后再组合在一起
完整代码
原本是下载tomcat10的,但是我本地jdk不符,最后下载的8
import lombok.SneakyThrows;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class AllDown {
// static String downUrl = "https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.19/src/apache-tomcat-10.1.19-src.zip";
static String downUrl = "https://dlcdn.apache.org/tomcat/tomcat-8/v8.5.99/bin/apache-tomcat-8.5.99-windows-x64.zip";
public static void main(String[] args) throws IOException {
long t1 = System.currentTimeMillis();
URL url = new URL(downUrl);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Accept-Encoding", "identity");
long size = conn.getContentLengthLong();
System.out.println("大小" + size);
Map<Integer, byte[]> map = new HashMap<>();
long start = -1;
long end = -1;
int index = 0;
List<Thread> list = new ArrayList<>();
while (end < size) {
start = end;
end += 1000000;
if (end >= size) {
end = size;
}
System.out.println("start" + start + "," + "end" + end);
list.add(new Thread(new Run(start+1,end,index,map)));
index++;
}
list.forEach(e->e.start());
list.forEach(e-> {
try {
e.join();
} catch (InterruptedException interruptedException) {
interruptedException.printStackTrace();
}
});
System.out.println("map " + map);
OutputStream out = new FileOutputStream("tomcat.zip");
for(int i=0;i<map.size();i++){
out.write(map.get(i));
}
out.flush();
out.close();
long t2 = System.currentTimeMillis();
System.out.println("耗费时间" + (t2-t1) + "ms");
}
}
class Run implements Runnable {
long start;
long end;
Integer index;
Map<Integer, byte[]> map;
public Run(long start,
long end,
Integer index,
Map<Integer, byte[]> map) {
this.start = start;
this.end = end;
this.index = index;
this.map = map;
}
@SneakyThrows
@Override
public void run() {
URL url = new URL(AllDown.downUrl);
URLConnection conn = url.openConnection();
conn.setRequestProperty("Range", "bytes=" + start + "-" + end);
InputStream in = conn.getInputStream();
int len = -1;
byte[] buff = new byte[2048];
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((len = in.read(buff)) > -1) {
out.write(buff,0,len);
}
out.flush();
map.put(index, out.toByteArray());
out.close();
in.close();
}
}
日志
大小12482200
start-1,end999999
start999999,end1999999
start1999999,end2999999
start2999999,end3999999
start3999999,end4999999
start4999999,end5999999
start5999999,end6999999
start6999999,end7999999
start7999999,end8999999
start8999999,end9999999
start9999999,end10999999
start10999999,end11999999
start11999999,end12482200
map {0=[B@7e9131d5, 1=[B@2e1d27ba, 2=[B@61d6015a, 3=[B@2525ff7e, 4=[B@524d6d96, 5=[B@152aa092, 6=[B@44a7bfbc, 7=[B@4ef37659, 8=[B@776b83cc, 9=[B@37858383, 10=[B@4e268090, 11=[B@1bb266b3, 12=[B@306cf3ea}
耗费时间33111ms
本地浏览器下载
还是有一些提升的
最后
解压打开后