import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.acl.LastOwnerException;
import javax.print.attribute.standard.Finishings;
public class Main {
static String path = "http://192.168.1.1/test.exe";
static int threadCount = 3;
static int finishedThread = 0;
public static void main(String[] args) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);
if(conn.getResponseCode() == 200){
int length = conn.getContentLength();
//创建临时文件
File file = new File(getNameFromPath(path));
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
//设置临时文件大小与目标文件一致
raf.setLength(length);
raf.close();
//计算每个线程下载区间
int size = length / threadCount;
for (int id = 0; id < threadCount; id++) {
int startIndex = id * size;
int endIndex = (id + 1) * size - 1;
if(id == threadCount - 1){
endIndex = length - 1;
}
System.out.println("线程" + id + "下载的区间:" + startIndex + " ~ " + endIndex);
new DownLoadThread(id, startIndex, endIndex).start();
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String getNameFromPath(String path){
int index = path.lastIndexOf("/");
return path.substring(index + 1);
}
}
class DownLoadThread extends Thread{
int threadId;
int startIndex;
int endIndex;
public DownLoadThread(int threadId, int startIndex, int endIndex) {
super();
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
File fileProgress = new File(threadId + ".txt");
int lastProgress = 0;
if(fileProgress.exists()){
FileInputStream fis = new FileInputStream(fileProgress);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
//得到上一次下载进度
lastProgress = Integer.parseInt(br.readLine());
startIndex += lastProgress;
fis.close();
}
URL url = new URL(Main.path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(8000);
conn.setReadTimeout(8000);
//设置请求数据的区间
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex);
if(conn.getResponseCode() == 206){
InputStream is = conn.getInputStream();
byte[] b = new byte[1024];
int len = 0;
//当前下载的总进度
int total = lastProgress;
File file = new File(Main.getNameFromPath(Main.path));
RandomAccessFile raf = new RandomAccessFile(file, "rwd");
raf.seek(startIndex);
while((len = is.read(b)) != -1){
raf.write(b, 0, len);
total += len;
System.out.println(threadId + "下载了:" + total);
//创建一个进度临时文件,保存下载进度
RandomAccessFile rafProgress = new RandomAccessFile(fileProgress, "rwd");
rafProgress.write((total + "").getBytes());
rafProgress.close();
}
raf.close();
System.out.println(threadId + "下载完毕------------------");
//3部分全部下载完毕,才删
Main.finishedThread++;
synchronized (Main.path) {
if(Main.finishedThread == Main.threadCount){
for (int i = 0; i < Main.threadCount; i++) {
File f = new File(i + ".txt");
f.delete();
}
Main.finishedThread = 0;
}
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
}
multithreaddownload
最新推荐文章于 2024-04-10 09:57:38 发布