Java-->多线程断点续传

时间:2022-01-01 20:34:23

--> 在多线程复制的基础上加入断点续传的功能

-->Test 测试类

 package com.dragon.java.multithreaddownload;

 import java.io.File;

 /*
* 多线程断点续传(下载)
*/
public class Test {
public static void main(String[] args) {
// @SuppressWarnings("resource")
// Scanner scanner = new Scanner(System.in);
// System.out.println("请输入文件路径:");
// 直接写死文件和线程数..
File srcFile = new File("F:/mp4/01_Java.mp4");
// System.out.println("请输入线程数:");
// int n = scanner.nextInt();
int n = 4;
if (!srcFile.exists()) {
System.out.println("该文件不存在!");
} File desFile = new File(srcFile.getParent(), "new" + srcFile.getName()); // 单线程复制长度
long partLenghth = srcFile.length() / n + 1;
for (int i = 1; i < n + 1; i++) {
// 启动线程
new MyThread(srcFile, desFile, partLenghth * (i - 1), partLenghth
* i).start(); }
}
}

--> MyThread 线程实现类

 package com.dragon.java.multithreaddownload;

 import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile; public class MyThread extends Thread {
private File srcFile;
private File desFile;
private long start;
private long end; MyThread() {
super();
} MyThread(File srcFile, File desFile, long start, long end) {
super();
this.srcFile = srcFile;
this.desFile = desFile;
this.start = start;
this.end = end;
} @Override
public void run() {
// 创建配置文件存储中断时的文件指针
File configFile = new File(desFile.getParent(), Thread.currentThread()
.getId() + ".config");
if (!configFile.exists() && desFile.exists()) { System.out.println(Thread.currentThread().getName() + "已经完成了下载");
return;
}
// 将流写在try() 中可以不需手动关闭流
try (RandomAccessFile rafSrc = new RandomAccessFile(srcFile, "r");
RandomAccessFile rafDes = new RandomAccessFile(desFile, "rw");
RandomAccessFile rafConfig = new RandomAccessFile(configFile,
"rw");) { rafConfig.setLength(8); // 当不是第一次下载时,将配置文件中的指针传递给开始指针
long pointer = rafConfig.readLong();
if (pointer != 0) {
start = pointer;
}
rafSrc.seek(start);
rafDes.seek(start); // 显示完成度 System.out.println(Thread.currentThread().getName() + "已下载:"
+ ((float) pointer / srcFile.length()) * 100 + "%"); int len = -1;
byte[] buffer = new byte[32];
while ((len = rafSrc.read(buffer)) != -1) {
rafDes.write(buffer, 0, len);
pointer = rafSrc.getFilePointer();
rafConfig.seek(0);
rafConfig.writeLong(pointer);
// 当每个线程完成任务时结束线程
if (rafSrc.getFilePointer() >= end) {
break;
}
}
} catch (IOException e) {
System.out.println(e);
}
// 删除配置文件
configFile.delete();
}
}