最近項目中新增了一個需求:要求批量下載之前信息導出的word 文件,也就是批量下載。
數據是保存在ES 中,導出word 的功能也是寫好的。
我的思路:
- 傳入數據ID分別導出word 文件到服務器某個文件夾。
- 根據文件地址將所有文件打包成zip文件到服務器某個地址
- 根據zip文件地址進行下載
存在問題:
批量導出word 文檔,由於數據量大可能會非常耗時。
解決:
採用的異步方式分別導出每一個word文件,大大節省了文件導出效率。
具體方法參考:Spring boot 入門教程- 使用異步線程池
使用這種方法雖然提高文件導出效率,但是所有文件導出完成時間無法控制。
這裏採用如下方法
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.elens.util
* @Author: xuweichao
* @CreateTime: 2019-06-04 15:51
* @Description: 文件壓縮工具類
*/
public class ZipUtil {
private static final int BUFFER_SIZE = 2 * 1024;
/**
* 壓縮成ZIP 方法1
*
* @param srcDir 壓縮文件夾路徑
* @param out 壓縮文件輸出流
* @param keepDirStructure 是否保留原來的目錄結構,true:保留目錄結構;
* false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名文件,會壓縮失敗)
* @throws RuntimeException 壓縮失敗會拋出運行時異常
*/
public static void toZip(String srcDir, OutputStream out, boolean keepDirStructure)
throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile, zos, sourceFile.getName(), keepDirStructure);
long end = System.currentTimeMillis();
System.out.println("壓縮完成,耗時:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 壓縮成ZIP 方法2
*
* @param srcFiles 需要壓縮的文件列表
* @param out 壓縮文件輸出流
* @throws RuntimeException 壓縮失敗會拋出運行時異常
*/
public static void toZip(List<File> srcFiles, OutputStream out) throws RuntimeException {
long start = System.currentTimeMillis();
ZipOutputStream zos = null;
try {
zos = new ZipOutputStream(out);
for (File srcFile : srcFiles) {
byte[] buf = new byte[BUFFER_SIZE];
zos.putNextEntry(new ZipEntry(srcFile.getName()));
int len;
FileInputStream in = new FileInputStream(srcFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
zos.closeEntry();
in.close();
}
long end = System.currentTimeMillis();
System.out.println("壓縮完成,耗時:" + (end - start) + " ms");
} catch (Exception e) {
throw new RuntimeException("zip error from ZipUtils", e);
} finally {
if (zos != null) {
try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 遞歸壓縮方法
*
* @param sourceFile 源文件
* @param zos zip輸出流
* @param name 壓縮後的名稱
* @param keepDirStructure 是否保留原來的目錄結構,true:保留目錄結構;
* false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名文件,會壓縮失敗)
* @throws Exception
*/
private static void compress(File sourceFile, ZipOutputStream zos, String name,
boolean keepDirStructure) throws Exception {
byte[] buf = new byte[BUFFER_SIZE];
if (sourceFile.isFile()) {
// 向zip輸出流中添加一個zip實體,構造器中name爲zip實體的文件的名字
zos.putNextEntry(new ZipEntry(name));
// copy文件到zip輸出流中
int len;
FileInputStream in = new FileInputStream(sourceFile);
while ((len = in.read(buf)) != -1) {
zos.write(buf, 0, len);
}
// Complete the entry
zos.closeEntry();
in.close();
} else {
File[] listFiles = sourceFile.listFiles();
if (listFiles == null || listFiles.length == 0) {
// 需要保留原來的文件結構時,需要對空文件夾進行處理
if (keepDirStructure) {
// 空文件夾的處理
zos.putNextEntry(new ZipEntry(name + "/"));
// 沒有文件,不需要文件的copy
zos.closeEntry();
}
} else {
for (File file : listFiles) {
// 判斷是否需要保留原來的文件結構
if (keepDirStructure) {
// 注意:file.getName()前面需要帶上父文件夾的名字加一斜槓,
// 不然最後壓縮包中就不能保留原來的文件結構,即:所有文件都跑到壓縮包根目錄下了
compress(file, zos, name + "/" + file.getName(), keepDirStructure);
} else {
compress(file, zos, file.getName(), keepDirStructure);
}
}
}
}
}
public static void main(String[] args) throws Exception {
/** 測試壓縮方法1 */
FileOutputStream fos1 = new FileOutputStream(new File("H:\\xuweichao.zip"));
ZipUtil.toZip("H:\\docTmp", fos1, true);
// /** 測試壓縮方法2 */
// List<File> fileList = new ArrayList<>();
// fileList.add(new File("H:\\docTmp\\36c194f1-6d69-4114-a26e-c6170af.doc"));
// fileList.add(new File("H:\\docTmp\\303e9dd9-6270-4968-95c8-13c660a.doc"));
// fileList.add(new File("H:\\docTmp\\545a857a-2f3b-487c-8b21-a58da9d.doc"));
// fileList.add(new File("H:\\docTmp\\ce608bff-f00d-4436-9c30-da73f70.doc"));
// FileOutputStream fos2 = new FileOutputStream(new File("H:\\docTmp\\xuweichao.zip"));
// ZipUtil.toZip(fileList, fos2);
}
}
調用
String zipFileName = "收藏夾人物信息打包-" + System.currentTimeMillis() + ".zip";
String zipFilePathName = words2ZipTmpDir + zipFileName;
File zipfile = new File(zipFilePathName);
if (!zipfile.getParentFile().exists()) {
zipfile.getParentFile().mkdirs();
}
FileOutputStream fos = new FileOutputStream(zipfile);
ZipUtil.toZip(zipFiles, fos);
List<File> zipFiles = new ArrayList<>();
zipFiles :需要打包的文件集合.
然後根據zipFilePathName 下載zip文件即可。
這裏只提供了實現思路和關鍵代碼,有問題歡迎反饋交流。