Spring Boot 文件通过zip压缩包 批量下载,附高频面试题合集

本文介绍了如何在Spring Boot应用中使用CompletableFuture进行并行文件导出,然后利用ZipUtil工具类批量压缩文件为zip。此外,文章还分享了作者的个人经历和Java开发者需要掌握的知识点,包括面试中常问的MySQL问题及Spring、Mybatis等框架的学习。
摘要由CSDN通过智能技术生成

List<CompletableFuture<Map<String, String>>> list = new ArrayList<>();

for (String sourceCode : sourceCodes.split(“,”)) {

CompletableFuture<Map<String, String>> future = favoriteService.exportFavoriteItemWordFile(sourceCode);

list.add(future);

}

CompletableFuture<Map<String, String>>[] completableFutures = list.toArray(new CompletableFuture[list.size()]);

CompletableFuture.allOf(completableFutures).join();

CompletableFuture.allOf().join(); 等待所有线程任务结束

文件导出方法调用:

@Async

@Override

public CompletableFuture<Map<String, String>> exportFavoriteItemWordFile(String sourceCode) throws IOException, URISyntaxException, InterruptedException {

Map<String, String> result = esDetailedService.exportDetailInfoDoc(sourceCode);

return CompletableFuture.completedFuture(result);

}

这里result 返回了文件在服务器上的存路径。可以通过future.get().get(“url”) 获取。

word 导出实现参考:使用POI 导出word模板文件

剩下的就是文件打包成zip 的解决了,这里提供一个工具类:

import java.io.*;

import java.util.ArrayList;

import java.util.List;

import java.util.zip.ZipEntry;

import java.util.zip.ZipOutputStream;

/**

  • @BelongsProject: exchange

  • @BelongsPackage: com.e

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值