public class DownLoad {
private static long blockSize;
private static int threadCount = 3;
private static int runningthreadcount;
private static long size;
private static String fileName;
public static void main(String[] args) {
String path = "http://88.2.250.143:8080/service/apache-tomcat-7.0.25-windows-x86.zip";
//得到文件名
fileName = getFileName(path);
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setReadTimeout(1000);
conn.setRequestMethod("GET");
int code = conn.getResponseCode();
if(code == 200){
//得到文件大小
size = conn.getContentLength();
blockSize = size/threadCount;
System.out.println("文件大小:"+size+",每块下载大小:"+blockSize);
//创建同样大小的文件
File file = new File(fileName);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(size);
runningthreadcount = threadCount;
//创建线程进行下载
for(int i=1;i<=threadCount;i++){
long startIndex = (i - 1) * blockSize;
long endIndex = i * blockSize -1;
if(i == threadCount){
endIndex = size - 1;
}
System.out.println("开始位置:"+startIndex+",结束位置:"+endIndex);
new ThraeadDownLoad(path,i,startIndex,endIndex).start();
}
conn.disconnect();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getFileName(String path){
String fileName = null;
if(path != null){
int position = path.lastIndexOf("/") + 1;
fileName = path.substring(position);
return fileName;
}
return fileName;
}
public static class ThraeadDownLoad extends Thread{
private int threadId;
private long startIndex;
private long endIndex;
private String path;
public ThraeadDownLoad(String path,int threadId,long startIndex,long endIndex){
this.path = path;
this.threadId = threadId;
this.startIndex = startIndex;
this.endIndex = endIndex;
}
@Override
public void run() {
try {
long total = 0;
//判断是否为断点下载
File positonFile = new File(threadId+".txt");
if(positonFile.exists() && positonFile.length() > 0){
//文件存在
FileReader fr = new FileReader(positonFile);
BufferedReader br = new BufferedReader(fr);
long lastCount = Integer.parseInt(br.readLine());
startIndex += lastCount;
total += lastCount;
fr.close();
}
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//断点下载用到的关键请求属性设置Range
conn.setRequestProperty("Range", "bytes="+startIndex+"-"+endIndex);
conn.setReadTimeout(1000);
conn.setRequestMethod("GET");
conn.getResponseCode();
InputStream is = conn.getInputStream();
byte[] buffer = new byte[1024*10];
int len = 0;
File file = new File(fileName);
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(startIndex);
while((len=is.read(buffer))!=-1){
//将下载进度记录到文本文件需用到RandomAccessFile,并将模式设置为rwd,因为下载需要先在缓冲区
//"rwd" 打开以便读取和写入,对于 "rw",还要求对文件内容的每个更新都同步写入到底层存储设备。
RandomAccessFile rf = new RandomAccessFile(positonFile, "rwd");
raf.write(buffer, 0, len);
total += len;
rf.write(String.valueOf(total).getBytes());
rf.close();
}
is.close();
raf.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
synchronized (DownLoad.class) {
runningthreadcount --;
if(runningthreadcount < 1){
for(int i = 1;i <= threadCount;i++){
File file = new File(i+".txt");
file.delete();
}
}
}
}
}
}
}
java实现多线程断点下载
最新推荐文章于 2021-02-21 17:30:45 发布