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,多线程,断点下载
0
投稿
猜你喜欢
C# 操作XML文档 使用XmlDocument类方法
2023-06-11 04:21:14
SpringBoot、mybatis返回树结构的数据实现
2022-05-12 18:56:08
android自定义控件实现简易时间轴(1)
2022-09-24 00:42:23
详解C#设置Excel数据自适应行高、列宽的2种情况
2022-10-21 23:46:29
C#8.0中的模式匹配
2023-07-19 13:27:39
Unity实现换装系统
2021-08-11 15:27:15
JAVA HashMap详细介绍和示例
2023-04-07 00:17:43
SpringBoot项目的配置文件中设置server.port不生效问题
2022-11-13 06:01:26
Java中的线程生命周期核心概念
2021-10-14 13:37:54
Android DataBinding布局的加载深入探究
2023-02-26 08:45:52
支持SpEL表达式的自定义日志注解@SysLog介绍
2023-08-27 09:38:42
基于Spring Mvc实现的Excel文件上传下载示例
2022-01-22 02:02:56
实例详解Java调用第三方接口方法
2023-10-15 06:34:32
Android实现带圆环的圆形头像
2021-09-30 09:04:47
关于java中构造函数的一些知识详解
2023-11-27 22:09:11
Java编写实现九宫格应用
2021-12-22 21:01:06
Android中 webView调用JS出错的解决办法
2021-12-02 21:23:25
Spark学习笔记之Spark中的RDD的具体使用
2023-07-05 04:49:44
Spring Cloud Gateway整合sentinel 实现流控熔断的问题
2022-01-18 23:10:05
Android开发中计算器的sin、cos及tan值计算问题分析
2023-11-11 08:21:40