diff --git a/casic-metering-common/src/main/java/com/casic/missiles/utils/ZipUtils.java b/casic-metering-common/src/main/java/com/casic/missiles/utils/ZipUtils.java index 4d98b95..d0df3a7 100644 --- a/casic-metering-common/src/main/java/com/casic/missiles/utils/ZipUtils.java +++ b/casic-metering-common/src/main/java/com/casic/missiles/utils/ZipUtils.java @@ -1,10 +1,12 @@ package com.casic.missiles.utils; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; +import org.apache.commons.lang3.StringUtils; + +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -14,13 +16,58 @@ */ public class ZipUtils { + + public static void batchDownloadFiles(String rootFileName, HttpServletResponse response,String localFileDir) { + FileInputStream is = null; + BufferedOutputStream out = null; + String rootFilePath = rootFileName + ".zip"; + try { + response.reset(); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/zip;charset=utf-8"); + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(rootFilePath, StandardCharsets.UTF_8.name())); + response.setHeader("Access-Control-Allow-Origin", "*"); + System.out.println(response.getHeader("Content-Disposition")); + rootFilePath =zipFileCreator(localFileDir, rootFileName, rootFilePath); + //开始下载 + is = new FileInputStream(new File(rootFilePath)); + out = new BufferedOutputStream(response.getOutputStream()); + byte[] buff = new byte[1024]; + int len = 0; + while ((len = is.read(buff, 0, buff.length)) != -1) { + out.write(buff, 0, len); + } + out.flush(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (out != null) { + try { + out.close(); + if (StringUtils.isNotEmpty(rootFilePath)) { + new ZipUtils().zipFileDeletor(rootFilePath); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + } /** * 批量打包 * * @param downloadPath 项目根目录 * @return tempDir 文件保存绝对路径 */ - public String zipFileCreator(String downloadPath, String tempDir,String papersZipName) { + public static String zipFileCreator(String downloadPath, String tempDir,String papersZipName) { ZipOutputStream out = null; try { //zip文件保存路径 diff --git a/casic-metering-common/src/main/java/com/casic/missiles/utils/ZipUtils.java b/casic-metering-common/src/main/java/com/casic/missiles/utils/ZipUtils.java index 4d98b95..d0df3a7 100644 --- a/casic-metering-common/src/main/java/com/casic/missiles/utils/ZipUtils.java +++ b/casic-metering-common/src/main/java/com/casic/missiles/utils/ZipUtils.java @@ -1,10 +1,12 @@ package com.casic.missiles.utils; -import java.io.File; -import java.io.FileInputStream; -import java.io.FileOutputStream; -import java.io.IOException; +import org.apache.commons.lang3.StringUtils; + +import javax.servlet.http.HttpServletResponse; +import java.io.*; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -14,13 +16,58 @@ */ public class ZipUtils { + + public static void batchDownloadFiles(String rootFileName, HttpServletResponse response,String localFileDir) { + FileInputStream is = null; + BufferedOutputStream out = null; + String rootFilePath = rootFileName + ".zip"; + try { + response.reset(); + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/zip;charset=utf-8"); + response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(rootFilePath, StandardCharsets.UTF_8.name())); + response.setHeader("Access-Control-Allow-Origin", "*"); + System.out.println(response.getHeader("Content-Disposition")); + rootFilePath =zipFileCreator(localFileDir, rootFileName, rootFilePath); + //开始下载 + is = new FileInputStream(new File(rootFilePath)); + out = new BufferedOutputStream(response.getOutputStream()); + byte[] buff = new byte[1024]; + int len = 0; + while ((len = is.read(buff, 0, buff.length)) != -1) { + out.write(buff, 0, len); + } + out.flush(); + } catch (Exception e) { + e.printStackTrace(); + } finally { + if (is != null) { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (out != null) { + try { + out.close(); + if (StringUtils.isNotEmpty(rootFilePath)) { + new ZipUtils().zipFileDeletor(rootFilePath); + } + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + } /** * 批量打包 * * @param downloadPath 项目根目录 * @return tempDir 文件保存绝对路径 */ - public String zipFileCreator(String downloadPath, String tempDir,String papersZipName) { + public static String zipFileCreator(String downloadPath, String tempDir,String papersZipName) { ZipOutputStream out = null; try { //zip文件保存路径 diff --git a/casic-metering-service/src/main/java/com/casic/missiles/service/Impl/quality/BizQualityTechnicalAnalysisRepServiceImpl.java b/casic-metering-service/src/main/java/com/casic/missiles/service/Impl/quality/BizQualityTechnicalAnalysisRepServiceImpl.java index 33a75c7..57bffb7 100644 --- a/casic-metering-service/src/main/java/com/casic/missiles/service/Impl/quality/BizQualityTechnicalAnalysisRepServiceImpl.java +++ b/casic-metering-service/src/main/java/com/casic/missiles/service/Impl/quality/BizQualityTechnicalAnalysisRepServiceImpl.java @@ -124,7 +124,7 @@ //所有线程并发执行 CompletableFuture.allOf(futuresArr).join(); //等待所有生成的线程结束,则进行数据的打包下载 - batchDownloadFiles(rootFileName, response); + ZipUtils.batchDownloadFiles(rootFileName, response,localFileDir); } @Override @@ -181,50 +181,4 @@ list.forEach(id -> this.removeById((Serializable) id)); return super.removeBatchByIds(list); } - - public void batchDownloadFiles(String rootFileName, HttpServletResponse response) { - FileInputStream is = null; - BufferedOutputStream out = null; - String rootFilePath = rootFileName + ".zip"; - try { - response.reset(); - response.setCharacterEncoding("UTF-8"); - response.setContentType("application/zip;charset=utf-8"); - response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(rootFilePath, StandardCharsets.UTF_8.name())); - response.setHeader("Access-Control-Allow-Origin", "*"); - System.out.println(response.getHeader("Content-Disposition")); - ZipUtils zipUtils = new ZipUtils(); - rootFilePath = zipUtils.zipFileCreator(localFileDir, rootFileName, rootFilePath); - //开始下载 - is = new FileInputStream(new File(rootFilePath)); - out = new BufferedOutputStream(response.getOutputStream()); - byte[] buff = new byte[1024]; - int len = 0; - while ((len = is.read(buff, 0, buff.length)) != -1) { - out.write(buff, 0, len); - } - out.flush(); - } catch (Exception e) { - e.printStackTrace(); - } finally { - if (is != null) { - try { - is.close(); - } catch (IOException e) { - e.printStackTrace(); - } - } - if (out != null) { - try { - out.close(); - if (StringUtils.isNotEmpty(rootFilePath)) { - new ZipUtils().zipFileDeletor(rootFilePath); - } - } catch (IOException e) { - e.printStackTrace(); - } - } - } - - } } \ No newline at end of file