所需数据

  1. 首先需要将所有需要合并的文件转换成字节流也就是byte[] 然后把,每个数据都放到一个list

文件路径转换成byte[]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
final List<String> collect = //你的文件路径集合
logger.info("开始转换pdf");

int totalUrls = collect.size();

int batchSize = 20; // 每批文件数量
int totalBatches = (int) Math.ceil((double) totalUrls / batchSize); // 共多少批任务
if (totalBatches <=0 ) return new byte[0];
ExecutorService executorService = //你的自定义线程池

List<CompletableFuture<byte[]>> futures = new ArrayList<>();

for (int i = 0; i < totalBatches; i++) {
int fromIndex = i * batchSize;
int toIndex = Math.min((i + 1) * batchSize, totalUrls);
List<String> batchUrls = collect.subList(fromIndex, toIndex);

for (String url : batchUrls) {
CompletableFuture<byte[]> future = CompletableFuture.supplyAsync(() -> {
logger.info("开始合并数据路径为:{}", url);

File imageFile = new File(url);
if (imageFile.exists() && imageFile.isFile()) {
try (InputStream fileInputStream = new FileInputStream(imageFile)) {
byte[] fileData = IOUtils.toByteArray(fileInputStream);
logger.info("文件大小为:{}", fileData.length);
return fileData;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/*if ((extName != null && (extName.toLowerCase().contains("png")
|| extName.toLowerCase().contains("jpg") || extName.toLowerCase().contains("jpeg")))) {
try {

final byte[] bytes = PicUtil.compressPictureForScale(url, 1024, 0.9);
logger.info("文件大小为:{}", bytes.length);
return bytes;
} catch (IOException e) {
throw new RuntimeException(e);
}
}*/
return null;
}, executorService);

futures.add(future);
}
}

List<byte[]> imageFiles = futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList());

executorService.shutdown();

上图中进行注释的地方是进行大图片压缩的,本文中所有涉及到的工具类都会在另一篇文章中附上源码