1
0
chenendian 2 дней назад
Родитель
Сommit
27994110ad

+ 22 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/controller/cadastre/ConvergeController.java

@@ -76,6 +76,28 @@ public class ConvergeController extends BaseController {
     }
     }
 
 
 
 
+
+
+
+    /**
+     * 下载汇交文件
+     * 支持多选,批量下载
+     * zip 文件
+     *
+     */
+    @PostMapping("/downConvergeMultiFile")
+    public void downConvergeMultiFile(HttpServletResponse response, @RequestBody IdsVo idsVo)  {
+        try {
+            convergeService.downConvergeMultiFile(response, idsVo.getIds());
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+
+
+
+
     /**
     /**
      * 下载汇交文件
      * 下载汇交文件
      * zip 文件
      * zip 文件

+ 3 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/mapper/ConvergeMapper.java

@@ -20,6 +20,9 @@ public interface ConvergeMapper {
      */
      */
     Converge get(String id);
     Converge get(String id);
 
 
+
+    List<Converge> getByIds(@Param("ids") List<String> ids);
+
     /**
     /**
      * 获取汇交记录列表
      * 获取汇交记录列表
      */
      */

+ 5 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/ConvergeService.java

@@ -37,4 +37,9 @@ public interface ConvergeService {
 
 
     void operationConverge(String batch,String type) ;
     void operationConverge(String batch,String type) ;
 
 
+
+    void downConvergeMultiFile(HttpServletResponse response, List<String> ids);
+
+
+
 }
 }

+ 55 - 1
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/service/impl/ConvergeServiceImpl.java

@@ -119,6 +119,7 @@ public class ConvergeServiceImpl implements ConvergeService {
     }
     }
 
 
 
 
+
     @Override
     @Override
     public void downConvergeFile(HttpServletResponse response, String ywh) {
     public void downConvergeFile(HttpServletResponse response, String ywh) {
         try {
         try {
@@ -142,7 +143,7 @@ public class ConvergeServiceImpl implements ConvergeService {
             response.setContentType("application/zip");
             response.setContentType("application/zip");
             response.setCharacterEncoding("utf-8");
             response.setCharacterEncoding("utf-8");
             String fileName = downName + ".zip";
             String fileName = downName + ".zip";
-            
+
             // 对文件名进行 URL 编码,解决中文乱码导致的 Header 非法字符异常
             // 对文件名进行 URL 编码,解决中文乱码导致的 Header 非法字符异常
             String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
             String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
             response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
             response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
@@ -161,6 +162,59 @@ public class ConvergeServiceImpl implements ConvergeService {
         }
         }
     }
     }
 
 
+
+
+
+    @Override
+    public void downConvergeMultiFile(HttpServletResponse response, List<String> idList) {
+        try {
+            if(CollectionUtils.isEmpty(idList)){
+                throw new ServiceException("汇交任务ID列表为空");
+            }
+            List<Converge> convergeList = convergeMapper.getByIds(idList);
+            if(CollectionUtils.isEmpty(convergeList)){
+                throw new ServiceException("获取不到当前任务:"+idList);
+            }
+
+            File[] zipFiles = new File[convergeList.size()];
+            int idx = 0;
+            String zipDirPath = convergedir;
+            for (Converge converge : convergeList) {
+                String path = converge.getFilePath();
+                Path dirPath = Paths.get(path);
+                if (!Files.exists(dirPath)) {
+                    throw new ServiceException("找不到汇交文件生成目录:"+converge.getId());
+                }
+                zipFiles[idx++] = dirPath.toFile();
+            }
+
+            String uuidStr = IdUtils.fastSimpleUUID();
+            File zipFile = new File(zipDirPath + uuidStr +".zip");
+            if(zipFiles.length>0){
+                ServiceFileUtil.zipByDir(zipFiles,null, zipFile);
+            }
+            // 4. 设置响应头并下载
+            String downName = uuidStr;
+            response.setContentType("application/zip");
+            response.setCharacterEncoding("utf-8");
+            String fileName = downName + ".zip";
+            // 对文件名进行 URL 编码,解决中文乱码导致的 Header 非法字符异常
+            String encodedFileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
+            response.setHeader("Content-Disposition", "attachment;filename=" + encodedFileName);
+            try (FileInputStream fis = new FileInputStream(zipFile)) {
+                byte[] buffer = new byte[4096];
+                int len;
+                while ((len = fis.read(buffer)) > 0) {
+                    response.getOutputStream().write(buffer, 0, len);
+                }
+            }
+            response.getOutputStream().flush();
+        } catch (Exception e) {
+            log.error("zip文件下载失败", e);
+            throw new RuntimeException("zip文件下载失败: " + e.getMessage());
+        }
+    }
+
     /**
     /**
      *
      *
      * 不动产汇交数据生成主逻辑
      * 不动产汇交数据生成主逻辑

+ 47 - 0
siwei-modules/siwei-apply/src/main/java/com/siwei/apply/utils/ServiceFileUtil.java

@@ -240,6 +240,53 @@ public class ServiceFileUtil {
     }
     }
 
 
 
 
+
+    /**
+     * 压缩多个目录/文件到同一个zip包
+     * @param dirFiles 待压缩文件/目录数组,允许多个根目录
+     * @param entryName 统一根条目前缀;传null/空则使用各自文件名称作为根
+     * @param zipFile 输出zip文件
+     * @throws IOException IO异常
+     */
+    public static void zipByDir(File[] dirFiles, String entryName, File zipFile) throws IOException {
+        // 参数校验
+        if (dirFiles == null || dirFiles.length == 0) {
+            throw new RuntimeException("待压缩文件数组不能为空");
+        }
+        for (File file : dirFiles) {
+            if (file == null || !file.exists()) {
+                throw new RuntimeException("源文件或目录不存在:" + (file == null ? "null" : file.getAbsolutePath()));
+            }
+        }
+
+        // 统一前缀处理
+        String rootEntryPrefix = null;
+        if (entryName != null && !entryName.trim().isEmpty()) {
+            rootEntryPrefix = entryName.trim();
+            // 保证目录分隔符结尾
+            if (!rootEntryPrefix.endsWith("/")) {
+                rootEntryPrefix += "/";
+            }
+        }
+
+        try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile), StandardCharsets.UTF_8)) {
+            if (rootEntryPrefix != null) {
+                // 全部多个目录放进同一个根文件夹 entryName/ 之下
+                for (File sourceFile : dirFiles) {
+                    String childEntry = rootEntryPrefix + sourceFile.getName();
+                    zipInternal(sourceFile, childEntry, zos);
+                }
+            } else {
+                // 不传根前缀:多个源直接平铺在zip根路径
+                for (File sourceFile : dirFiles) {
+                    zipInternal(sourceFile, sourceFile.getName(), zos);
+                }
+            }
+        }
+    }
+
+
+
     /**
     /**
      * 通过目录进行压缩
      * 通过目录进行压缩
      * @param dirFile
      * @param dirFile

+ 10 - 0
siwei-modules/siwei-apply/src/main/resources/mapper/ConvergeMapper.xml

@@ -30,6 +30,16 @@
         WHERE id = #{id}
         WHERE id = #{id}
     </select>
     </select>
 
 
+    <select id="getByIds" resultMap="convergeMap">
+        SELECT *
+        FROM t_converge
+        WHERE id IN
+        <foreach collection="ids" item="id" open="(" separator="," close=")">
+            #{id}
+        </foreach>
+    </select>
+
+
     <select id="getList" parameterType="com.siwei.apply.domain.vo.ConvergeFilterVo" resultMap="convergeMap">
     <select id="getList" parameterType="com.siwei.apply.domain.vo.ConvergeFilterVo" resultMap="convergeMap">
         SELECT *
         SELECT *
         FROM t_converge
         FROM t_converge