大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
首先,我们要下载一个文件,可以通过多线程的方式快速下载!!!
多线程下载文件的步骤:
1、首先要知道请求下载的服务器支持断点下载,即支持request头信息中的Range的设置
2、然后通过对请求头设置
httpConnection.setRequestProperty("Range","bytes="+startIndex+"-"+endIndex);
3、然后获取整个文件的大小
4、在本地创建一个一样大的文件,然后根据线程数进行分配startIndex和endIndex
5、线程下载的同时,使用RandomAccessFile对所下载的内容随机对应写入文件
这里要注意,你所访问的文件在服务器端必须吧能够返回Content-Length这个参数才行!!!
完整的示例代码:
package cn.ljxwtl.plugin.common.util;
import com.alibaba.fastjson.JSON;
import cn.ljxwtl.plugin.engine.EngineAssistant;
import org.apache.commons.io.IOUtils;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.RandomAccess;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author: wtl
* @License: (C) Copyright 2022, wtl Corporation Limited.
* @Contact: 1050100468@qq.com
* @Date: 2022/4/1 10:50
* @Version: 1.0
* @Description:
*/
public class DownloadFileUtil {
public static void downloadFileWithMultiThread(String url,String filePath,Runnable runnable) throws Exception {
List<long[]> perThreadDownloadFileOffset = getPerThreadDownloadFileOffset(url, 1024* 1024);
ThreadPoolExecutor threadPoolExecutor = EngineAssistant.THREAD_POOL_EXECUTOR;
int size = Objects.requireNonNull(perThreadDownloadFileOffset).size();
CyclicBarrier cyclicBarrier = new CyclicBarrier(size, ()->{
if (null != runnable){
runnable.run();
}
});
URL openUrl = new URL(url);
for (int i = 0; i< size; i++){
int finalI = i;
new Thread(()->{
try {
HttpURLConnection urlConnection = (HttpURLConnection) openUrl.openConnection();
urlConnection.setRequestProperty("Range","bytes="+perThreadDownloadFileOffset.get(finalI)[0]+"-"+perThreadDownloadFileOffset.get(finalI)[1]);
urlConnection.setDoInput(true);
BufferedInputStream bufferedInputStream = new BufferedInputStream(urlConnection.getInputStream());
int length = -1;
byte [] buffer = new byte[102400];
RandomAccessFile randomAccessFile = new RandomAccessFile(filePath,"rw");
System.out.println(perThreadDownloadFileOffset.get(finalI)[1] - perThreadDownloadFileOffset.get(finalI)[0]);
randomAccessFile.seek(perThreadDownloadFileOffset.get(finalI)[0]);
int perSize = 0;
while ((length = bufferedInputStream.read(buffer))!=-1){
randomAccessFile.write(buffer,0,length);
perSize +=length;
}
randomAccessFile.close();
System.out.println(perThreadDownloadFileOffset.get(finalI)[0] + "------------------" +(perThreadDownloadFileOffset.get(finalI)[0] + perSize));
} catch (Exception e) {
e.printStackTrace();
}
finally {
try {
cyclicBarrier.await();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}
/**
* 获取每个线程所需要的File的offset
* @param url url地址
* @param perThreadDealFileSize 每个线程处理的文件下载大小
* @return List<int []>
*/
private static List<long []> getPerThreadDownloadFileOffset(String url,long perThreadDealFileSize){
URLConnection urlConnection = null;
try {
urlConnection = new URL(url).openConnection();
long contentLengthLong = urlConnection.getContentLengthLong();
List<long[]> threadOffsetDatas = new ArrayList<>();
long size = (long) Math.ceil(contentLengthLong * 1.0 / perThreadDealFileSize);
for (int i = 0; i < size; i++) {
long start = i * perThreadDealFileSize;
long end = (i + 1) * perThreadDealFileSize - 1;
if (i == size - 1) {
start = i * perThreadDealFileSize;
end = contentLengthLong;
}
threadOffsetDatas.add(new long[]{start,end});
}
System.out.println(JSON.toJSONString(threadOffsetDatas));
return threadOffsetDatas;
}
catch (Exception e){
e.printStackTrace();
}
return null;
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/195242.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...