车车 3 mēneši atpakaļ
vecāks
revīzija
a38572fdd9

+ 5 - 9
yudao-module-iscs/src/main/java/cn/iocoder/yudao/module/iscs/controller/admin/workflow/WorkflowModeController.java

@@ -22,11 +22,10 @@ import org.springframework.web.bind.annotation.*;
 import javax.crypto.BadPaddingException;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.NoSuchPaddingException;
-import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
-import java.util.ArrayList;
 import java.util.List;
 
 import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
@@ -61,7 +60,7 @@ public class WorkflowModeController {
     @DeleteMapping("/deleteWorkflowModeList")
     @Parameter(name = "ids", description = "编号", required = true)
     @Operation(summary = "批量删除工作流模式")
-                @PreAuthorize("@ss.hasPermission('iscs:workflow-mode:delete')")
+    @PreAuthorize("@ss.hasPermission('iscs:workflow-mode:delete')")
     public CommonResult<Boolean> deleteWorkflowModeList(@RequestParam("ids") List<Long> ids) {
         workflowModeService.deleteWorkflowModeListByIds(ids);
         return success(true);
@@ -88,12 +87,9 @@ public class WorkflowModeController {
     @Operation(summary = "导出工作流模式")
     @PreAuthorize("@ss.hasPermission('iscs:workflow-mode:export')")
     @ApiAccessLog(operateType = EXPORT)
-    public void exportWorkflowMode(
-              HttpServletResponse response) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, FileNotFoundException {
-        ArrayList<Long> objects = new ArrayList<>();
-        objects.add(1L);
-        objects.add(2L);
-        workflowModeService.exportWorkflowMode(response, objects);
+    public void exportWorkflowMode(@RequestParam("ids") List<Long> ids,
+                                   HttpServletResponse response) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException {
+        workflowModeService.exportWorkflowMode(response, ids);
 
 
     }

+ 2 - 2
yudao-module-iscs/src/main/java/cn/iocoder/yudao/module/iscs/service/workflow/WorkflowModeService.java

@@ -12,7 +12,7 @@ import jakarta.validation.Valid;
 import javax.crypto.BadPaddingException;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.NoSuchPaddingException;
-import java.io.FileNotFoundException;
+import java.io.IOException;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
@@ -70,7 +70,7 @@ public interface WorkflowModeService extends IService<WorkflowModeDO> {
      */
     PageResult<WorkflowModeRespVO> getWorkflowModePage(WorkflowModePageReqVO pageReqVO);
 
-    void exportWorkflowMode(HttpServletResponse response, List<Long> ids) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, FileNotFoundException;
+    void exportWorkflowMode(HttpServletResponse response, List<Long> ids) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException;
 
 
 }

+ 30 - 12
yudao-module-iscs/src/main/java/cn/iocoder/yudao/module/iscs/service/workflow/WorkflowModeServiceImpl.java

@@ -25,6 +25,9 @@ import javax.crypto.BadPaddingException;
 import javax.crypto.IllegalBlockSizeException;
 import javax.crypto.NoSuchPaddingException;
 import java.io.*;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.security.InvalidAlgorithmParameterException;
 import java.security.InvalidKeyException;
 import java.security.NoSuchAlgorithmException;
@@ -145,7 +148,7 @@ public class WorkflowModeServiceImpl extends ServiceImpl<WorkflowModeMapper, Wor
     }
 
     @Override
-    public void exportWorkflowMode(HttpServletResponse response, List<Long> ids) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException {
+    public void exportWorkflowMode(HttpServletResponse response, List<Long> ids) throws InvalidAlgorithmParameterException, NoSuchPaddingException, IllegalBlockSizeException, NoSuchAlgorithmException, BadPaddingException, InvalidKeyException, IOException {
         Assert.isFalse(ids.isEmpty(), "导出的数据不可为空!");
         // 开始查询数据
         List<WorkflowModeDO> workflowModeDOS = listByIds(ids);
@@ -162,6 +165,11 @@ public class WorkflowModeServiceImpl extends ServiceImpl<WorkflowModeMapper, Wor
         String s = AES256Utils.AES256DECRYPT(modeExportVOS.toString());
         // 时间戳
         long currentTimestamp = System.currentTimeMillis();
+        // 确保目录存在
+        Path uploadPath = Paths.get(getPath());
+        if (!Files.exists(uploadPath)) {
+            Files.createDirectories(uploadPath);
+        }
         // 开始写入dat文件
         String url = getPath() + currentTimestamp + ".dat";
         try (FileOutputStream fos = new FileOutputStream(url)) {
@@ -181,23 +189,33 @@ public class WorkflowModeServiceImpl extends ServiceImpl<WorkflowModeMapper, Wor
         }
 
         // 开始压缩
-        List<String> filesToZip = List.of(url, url1);
-        String outputZip = getPath() + currentTimestamp + ".zip";
-
-        try {
-            zipFiles(filesToZip, outputZip);
-            System.out.println("成功创建ZIP文件:" + outputZip);
+        response.setContentType("application/zip");
+        response.addHeader("Content-Disposition", "attachment;filename=" + HttpUtils.encodeUtf8(currentTimestamp + "zip"));
+        try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
+            // 动态添加文件到ZIP
+            addFileToZip(zos, currentTimestamp + ".dat", Files.readAllBytes(Paths.get(url)));
+            addFileToZip(zos, currentTimestamp + ".sha", Files.readAllBytes(Paths.get(url1)));
         } catch (IOException e) {
-            System.err.println("创建ZIP文件失败!");
-            e.printStackTrace();
+            response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "压缩失败");
+        } finally {
+            deleteFileByPath(url);
+            deleteFileByPath(url1);
         }
+    }
 
-        // 2. 设置 HTTP 响应头
-        response.addHeader("Content-Disposition", "attachment;filename=" + HttpUtils.encodeUtf8(currentTimestamp + ".zip"));
-        response.setContentType("application/zip");
+    private void deleteFileByPath(String path) {
+        File file = new File(path);
+        file.delete();
     }
 
 
+    private void addFileToZip(ZipOutputStream zos, String fileName, byte[] content) throws IOException {
+        ZipEntry entry = new ZipEntry(fileName);
+        zos.putNextEntry(entry);
+        zos.write(content);
+        zos.closeEntry();
+    }
+
     private String getPath() {
         // D:
         String driveLetter = FileDriveLetterUtils.getDriveLetter();