Java多线程下载文件实现案例详解
作者:yaominghui 时间:2023-11-09 14:02:36
原理解析:
利用RandomAccessFile在本地创建一个随机访问文件,文件大小和服务器要下载的文件大小相同。 根据线程的数量(假设有三个线程),服务器的文件三等分,并把我们在本地创建的文件同样三等分,每个线程下载自己负责的部分,到相应的位置即可。
示例图:
代码如下
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
public class MutilDownload {
private static String path = "http://192.168.80.85:8080/test.doc";
private static final int threadCount = 3;
public static void main(String[] args) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
int responseCode = conn.getResponseCode();
if (responseCode == 200) {
int contentLength = conn.getContentLength();
System.out.println("length" + contentLength);
RandomAccessFile rafAccessFile = new RandomAccessFile("test.doc", "rw");
rafAccessFile.setLength(contentLength);
int blockSize = contentLength / threadCount;
for (int i = 0; i < threadCount; i++) {
int startIndex = i * blockSize; //每个现成下载的开始位置
int endIndex = (i + 1) * blockSize - 1;// 每个线程的结束位置
if (i == threadCount - 1) {
//最后一个线程
endIndex = contentLength - 1;
}
new DownloadThread(startIndex, endIndex, i).start();
}
}
} catch (Exception e) {
}
}
private static class DownloadThread extends Thread {
private int startIndex;
private int endIndex;
private int threadId;
public DownloadThread(int startIndex, int endIndex, int threadId) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.threadId = threadId;
}
@Override
public void run() {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setRequestProperty("Range", "bytes=" + startIndex + "-" + endIndex); //固定写法,请求部分资源
int responseCode = conn.getResponseCode(); // 206表示请求部分资源
if (responseCode == 206) {
RandomAccessFile rafAccessFile = new RandomAccessFile("test.doc", "rw");
rafAccessFile.seek(startIndex);
InputStream is = conn.getInputStream();
int len = -1;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) != -1) {
rafAccessFile.write(buffer, 0, len);
}
rafAccessFile.close();
System.out.println("线程" + threadId + "下载完成");
}
} catch (Exception e) {
}
}
}
}
来源:https://www.cnblogs.com/myseries/p/12554084.html
标签:Java,多线程,下载,文件
0
投稿
猜你喜欢
Java使用反射创建对象示例
2023-10-14 11:31:44
java服务器的简单实现过程记录
2023-01-18 06:44:34
C#中decimal保留2位有效小数的实现方法
2023-01-30 07:35:13
解决MyEclipse中Maven设置jdk版本jdk1.8报错问题
2022-04-20 18:19:04
Java几个实例带你进阶升华下篇
2021-06-11 01:19:46
C++日期类计算器的模拟实现举例详解
2023-05-22 08:27:16
SpringBoot中使用Filter和Interceptor的示例代码
2022-06-28 17:20:04
Android双击事件拦截方法
2022-07-21 19:33:30
Android提高Service优先级的方法分析
2023-01-09 09:05:51
Spring的@RequestParam对象绑定方式
2023-02-03 21:05:35
Android沉浸式状态栏实现
2022-11-23 12:08:12
C#中事务处理和非事务处理方法实例分析
2023-12-23 08:09:13
教你怎么用Idea打包jar包
2023-03-15 03:30:51
java 单例的五种实现方式及其性能分析
2023-05-10 10:50:51
使用springboot整合RateLimiter限流过程
2022-09-12 21:42:48
如何利用反射生成 MyBatisPlus中QueryWrapper动态条件
2021-10-20 14:59:40
详解Java中HashSet和TreeSet的区别
2022-01-17 09:47:47
Java Valhalla Project项目介绍
2021-10-03 00:29:28
LRU缓存替换策略及C#实现方法分享
2021-08-27 04:20:49
Flutter web bridge 通信总结分析详解
2022-05-23 05:15:42