Java实现多线程断点下载
作者:lijiao 时间:2022-04-27 00:29:16
JAVA多线程断点下载原理如图:
代码如下:
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.URL;
public class MutileThreadDownload {
/**
* 线程的数量
*/
private static int threadCount = 3;
/**
* 每个下载区块的大小
*/
private static long blocksize;
/**
* 正在运行的线程的数量
*/
private static int runningThreadCount;
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// 服务器文件的路径
String path = "http://192.168.1.100:8080/ff.exe";
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
if (code == 200) {
long size = conn.getContentLength();// 得到服务端返回的文件的大小
System.out.println("服务器文件的大小:" + size);
blocksize = size / threadCount;
// 1.首先在本地创建一个大小跟服务器一模一样的空白文件。
File file = new File("temp.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.setLength(size);
// 2.开启若干个子线程分别去下载对应的资源。
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("开启线程:" + i + "下载的位置:" + startIndex + "~"
+ endIndex);
new DownloadThread(path, i, startIndex, endIndex).start();
}
}
conn.disconnect();
}
private static class DownloadThread extends Thread {
private int threadId;
private long startIndex;
private long endIndex;
private String path;
public DownloadThread(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 {
// 当前线程下载的总大小
int total = 0;
File positionFile = new File(threadId + ".txt");
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url
.openConnection();
conn.setRequestMethod("GET");
// 接着从上一次的位置继续下载数据
if (positionFile.exists() && positionFile.length() > 0) {// 判断是否有记录
FileInputStream fis = new FileInputStream(positionFile);
BufferedReader br = new BufferedReader(
new InputStreamReader(fis));
// 获取当前线程上次下载的总大小是多少
String lasttotalstr = br.readLine();
int lastTotal = Integer.valueOf(lasttotalstr);
System.out.println("上次线程" + threadId + "下载的总大小:"
+ lastTotal);
startIndex += lastTotal;
total += lastTotal;// 加上上次下载的总大小。
fis.close();
}
conn.setRequestProperty("Range", "bytes=" + startIndex + "-"
+ endIndex);
conn.setConnectTimeout(5000);
int code = conn.getResponseCode();
System.out.println("code=" + code);
InputStream is = conn.getInputStream();
File file = new File("temp.exe");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
// 指定文件开始写的位置。
raf.seek(startIndex);
System.out.println("第" + threadId + "个线程:写文件的开始位置:"
+ String.valueOf(startIndex));
int len = 0;
byte[] buffer = new byte[512];
while ((len = is.read(buffer)) != -1) {
RandomAccessFile rf = new RandomAccessFile(positionFile,
"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 (MutileThreadDownload.class) {
System.out.println("线程" + threadId + "下载完毕了");
runningThreadCount--;
if (runningThreadCount < 1) {
System.out.println("所有的线程都工作完毕了。删除临时记录的文件");
for (int i = 1; i <= threadCount; i++) {
File f = new File(i + ".txt");
System.out.println(f.delete());
}
}
}
}
}
}
}
标签:java,多线程,断点下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java AQS信号量Semaphore的使用
2021-06-11 17:10:44
mybatis水平分表实现动态表名的项目实例
2023-04-02 22:11:42
![](https://img.aspxhome.com/file/2023/2/59772_0s.png)
Java 十大排序算法之计数排序刨析
2023-11-28 19:21:26
![](https://img.aspxhome.com/file/2023/3/68133_0s.png)
Java JVM内存区域详解
2021-05-25 18:44:43
![](https://img.aspxhome.com/file/2023/3/64893_0s.jpg)
Spring中Bean扫描原理详情
2022-05-26 04:33:14
![](https://img.aspxhome.com/file/2023/8/75488_0s.png)
如何解决java压缩文件乱码问题
2022-05-20 09:15:57
Java实现去除文档阴影的示例代码
2023-08-31 11:45:48
![](https://img.aspxhome.com/file/2023/5/58415_0s.png)
实现java简单的线程池
2023-08-09 06:05:15
![](https://img.aspxhome.com/file/2023/5/58065_0s.png)
Java实现接口的枚举类示例
2023-06-18 01:22:04
springboot配置文件绑定实现解析
2022-06-07 23:32:38
![](https://img.aspxhome.com/file/2023/7/61917_0s.png)
SpringBoot使用注解进行分页的实现示例
2021-08-21 22:45:32
![](https://img.aspxhome.com/file/2023/3/75473_0s.jpg)
浅谈Java内存区域划分和内存分配策略
2023-08-11 18:52:49
![](https://img.aspxhome.com/file/2023/6/58146_0s.png)
java读写oracle的blob字段示例
2023-12-22 16:19:00
详解Java变量与常量
2023-11-09 21:00:14
![](https://img.aspxhome.com/file/2023/5/59005_0s.png)
基于Spring Mvc实现的Excel文件上传下载示例
2022-01-22 02:02:56
WinForm中Application.Idle方法详解
2022-09-05 01:53:06
![](https://img.aspxhome.com/file/2023/5/69815_0s.jpg)
解决使用json-lib包实现xml转json时空值被转为空中括号的问题
2022-10-20 02:12:14
SpringBoot整合mybatis的方法详解
2023-09-02 06:23:57
![](https://img.aspxhome.com/file/2023/5/58455_0s.png)
Java Apache Shiro安全框架快速开发详解流程
2022-06-21 23:00:23
![](https://img.aspxhome.com/file/2023/8/69648_0s.png)
Qt之调用C#的动态库的解决方法
2023-07-22 03:42:45
![](https://img.aspxhome.com/file/2023/1/67061_0s.png)