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