public class MutiThradDownload {
private static int threadCount = 3;
private static String path = "http://192.168.10.157:8080/QQ.apk";
public static void main(String[] args) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if (code==200) {
int lenth = conn.getContentLength();
File file = new File(getFilename(path));
RandomAccessFile raf = new RandomAccessFile(file,"rw");
raf.setLength(lenth);
raf.close();
int blockSize = lenth/threadCount;
System.out.println("文件总大小为"+lenth);
for (int threadId = 0; threadId < threadCount; threadId++) {
int startIndex = threadId*blockSize;
int endIndex = (threadId+1)*blockSize-1;
if (threadId==threadCount-1) {
endIndex = lenth-1;
}
new DownloadPart(threadId, startIndex, endIndex).start();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static class DownloadPart extends Thread{
private int threadId;
private int startIndex;
private int endIndex;
private int currentPosition;
public DownloadPart (int threadId,int startIndex,int endIndex){
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
this.currentPosition = startIndex;
}
@Override
public void run() {
System.out.println("第"+threadId+"线程:开始下载了"+startIndex+"~"+endIndex);
try {
URL url= new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("GET");
File file = new File(getFilename(path));
RandomAccessFile raf = new RandomAccessFile(file, "rw");
conn.setRequestProperty("range", "bytes="+startIndex+"-"+endIndex);
System.out.println("----"+threadId);
File ilf = new File(threadId+".position");
if (ilf.exists()&&ilf.length()>0) {
System.out.println("之前下载过");
BufferedReader br = new BufferedReader(new FileReader(ilf));
String vl = br.readLine();
int alreadlywriteposition = Integer.parseInt(vl);
conn.setRequestProperty("range", "bytes="+alreadlywriteposition+"-"+endIndex);
raf.seek(alreadlywriteposition);
}else{
System.out.println("之前没有下载过");
conn.setRequestProperty("range", "bytes="+startIndex+"-"+endIndex);
raf.seek(startIndex);
}
int code = conn.getResponseCode();
if (code==206) {
InputStream in = conn.getInputStream();
int len = 0 ;
byte[] buf = new byte[1024];
while ((len=in.read(buf))>0) {
raf.write(buf,0,len);
currentPosition = currentPosition+len;
File info = new File(threadId+".position");
OutputStream out = new FileOutputStream(info);
out.write(String.valueOf(currentPosition).getBytes());
out.close();
}
raf.close();
in.close();
}
System.out.println("第"+threadId+"线程下载结束了");
synchronized (MutiThradDownload.class) {
currentcount--;
if (currentcount<=0){
File fff = new File(threadId+".position");
fff.renameTo(new File(threadId+".position"));
for (int threadid = 0; threadid <threadCount; threadid++) {
File fll = new File(threadId+".position");
fll.delete();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static String getFilename(String path){
int index = path.lastIndexOf("/");
return path.substring(index+1);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)