先发送请求探测是否支持分片下载,同时从响应头中获取Content-Range文件总大小
然后分片,获取到每页的开始结束位置
提交分片任务到线程池中,await等待所有分片任务下载完成,进行合并任务
基础bean:
@Data
public class SlicePageInfo {
private CopyOnWriteArrayList sliceInfoList;
private Long page;
}
@Data
@AllArgsConstructor
public class SliceInfo {
private long start;
private long end;
private long page;
}
@Slf4j
public class DownLoadSliceUtil {
public final static long PER_PAGE = (long) 1024 * 1024;
private static final RestTemplate REST_TEMPLATE = new RestTemplate();
public static ResponseEntity getFileContentByUrlAndPosition(String downloadUrl, long start, long end) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.set("Range", "bytes=" + start + "-" + end);
org.springframework.http.HttpEntity
@Slf4j
public class DownLoadEngine {
private static final ExecutorService executorService = ExecutorFactory.newFixedExecutorService(5);
public static void downloadSlice(String downloadUrl, String tempPath, String fileName) {
//大小探测
ResponseEntity responseEntity = DownLoadSliceUtil.getFileContentByUrlAndPosition(downloadUrl, 0, 1);
HttpHeaders headers = responseEntity.getHeaders();
String rangeBytes = headers.getFirst("Content-Range");
if (Objects.isNull(rangeBytes)) {
log.error("url:{},不支持分片下载", downloadUrl);
return;
}
long allBytes = Long.parseLong(rangeBytes.split("/")[1]);
log.info("文件总大小:{}M", allBytes / 1024.0 / 1024.0);
//分页
SlicePageInfo slicePageInfo = splitPage(allBytes);
CountDownLatch countDownLatch = new CountDownLatch(Math.toIntExact(slicePageInfo.getPage()));
CountDownLatch mainLatch = new CountDownLatch(1);
executorService.execute(() -> {
try {
countDownLatch.await();
DownLoadSliceUtil.mergeFileTranTo(tempPath, fileName, slicePageInfo.getPage());
mainLatch.countDown();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
for (SliceInfo sliceInfo : slicePageInfo.getSliceInfoList()) {
executorService.submit(() -> {
DownLoadSliceUtil.download(tempPath, downloadUrl, sliceInfo, fileName);
countDownLatch.countDown();
});
}
try {
mainLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static SlicePageInfo splitPage(long allBytes) {
CopyOnWriteArrayList list = new CopyOnWriteArrayList<>();
long size = allBytes;
long left = 0;
long page = 0;
while (size > 0) {
long start = 0;
long end;
start = left;
//分页
if (size < PER_PAGE) {
end = left + size;
} else {
end = left += PER_PAGE;
}
size -= PER_PAGE;
page++;
if (start != 0) {
start++;
}
log.info("页码:{},开始位置:{},结束位置:{}", page, start, end);
final SliceInfo sliceInfo = new SliceInfo(start, end, page);
list.add(sliceInfo);
}
SlicePageInfo slicePageInfo = new SlicePageInfo();
slicePageInfo.setSliceInfoList(list);
slicePageInfo.setPage(page);
return slicePageInfo;
}
}
测试
@Test
public void downloadFile() {
DownLoadEngine.downloadSlice("https://dldir1.qq.com/qqfile/qq/PCQQ9.6.1/QQ9.6.1.28732.exe", "D:temp", "qq.exe");
}



