瀏覽代碼

fix:优化完善下载工程文件接口,在下载完成之后删除租户的数据文件

韩帛霖 1 年之前
父節點
當前提交
288c093ad1

+ 158 - 0
zkqy-admin/src/main/java/com/zkqy/web/controller/projcetzip/DownloadController.java

@@ -0,0 +1,158 @@
+package com.zkqy.web.controller.projcetzip;
+
+import com.zkqy.common.config.ZkqyConfig;
+import com.zkqy.common.utils.SecurityUtils;
+import com.zkqy.common.utils.ZipUtils;
+import com.zkqy.system.service.ISysEngineeringService;
+import org.apache.tools.zip.ZipEntry;
+import org.apache.tools.zip.ZipOutputStream;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import javax.servlet.http.HttpServletResponse;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.locks.ReentrantLock;
+
+@RestController
+@RequestMapping("/download/project-db")
+public class DownloadController {
+
+    @Autowired
+    private ISysEngineeringService sysEngineeringService;
+
+    @Autowired
+    private ZkqyConfig zkqyConfig;
+    private static final ReentrantLock lock = new ReentrantLock();
+    private volatile boolean exportCompleted = false;
+
+
+    @GetMapping("/status")
+    public ResponseEntity<String> checkExportStatus() {
+        lock.lock();
+        try {
+            if (exportCompleted) {
+                return ResponseEntity.ok("导出已完成。您现在可以触发下载");
+            } else {
+                return ResponseEntity.status(HttpStatus.GATEWAY_TIMEOUT).body("文件还在生成中。请稍候。。。");
+            }
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    @GetMapping("/download")
+    public void downloadZip(HttpServletResponse response) {
+        lock.lock();
+        System.err.println(lock);
+        String sqlDirectoryPath = zkqyConfig.getUploadPath() + "/engineeringdownload/sql/" + SecurityUtils.getTenantId();
+        String jarDirectoryPath = zkqyConfig.getUploadPath() + "/engineeringdownload/jar/";
+        try {
+            if (exportCompleted) {
+                try {
+                    List<Map<String, String>> fileList = listFiles(sqlDirectoryPath, jarDirectoryPath);
+                    try {
+                        response.setContentType("application/x-octet-stream");
+                        response.setHeader("Content-Disposition", "attachment; filename=\"download.zip\"");
+                        response.setCharacterEncoding("utf-8");
+                        ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
+                        byte[] buffer = new byte[1024];
+                        for (Map<String, String> fileMap : fileList) {
+                            String fileName = fileMap.get("fileName");
+                            String filePath = fileMap.get("filePath");
+                            // 使用 File.separator 来实现跨平台兼容性
+                            String relativePath = fileName;
+                            // 在 Zip 中创建带有相对路径的 ZipEntry
+                            zos.putNextEntry(new ZipEntry(relativePath));
+                            try (InputStream is = new FileInputStream(filePath)) {
+                                int length;
+                                while ((length = is.read(buffer)) > 0) {
+                                    zos.write(buffer, 0, length);
+                                }
+                            } catch (IOException e) {
+                                e.printStackTrace();
+                            }
+                            zos.closeEntry();
+                        }
+                        zos.close();
+                        zos.flush();
+                    } catch (IOException e) {
+                        e.printStackTrace();
+                    }
+                    // 下载完成后删除sqlDirectoryPath路径下的所有文件
+                    deleteFilesInDirectory(sqlDirectoryPath);
+                } catch (Exception e) {
+                    e.printStackTrace();
+                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
+                }
+            } else {
+                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
+            }
+        } finally {
+            lock.unlock();
+        }
+    }
+
+
+    private List<Map<String, String>> listFiles(String sqlDirectoryPath, String jarDirectoryPath) {
+        List<Map<String, String>> fileList = new ArrayList<>();
+        processDirectory(sqlDirectoryPath, fileList);
+        processDirectory(jarDirectoryPath, fileList);
+        return fileList;
+    }
+
+    private void processDirectory(String directoryPath, List<Map<String, String>> fileList) {
+        File directory = new File(directoryPath);
+        if (directory.exists() && directory.isDirectory()) {
+            listFilesRecursively(directory, fileList);
+        }
+    }
+
+    private void listFilesRecursively(File directory, List<Map<String, String>> fileList) {
+        File[] files = directory.listFiles();
+        if (files != null) {
+            for (File file : files) {
+                if (file.isDirectory()) {
+                    listFilesRecursively(file, fileList);
+                } else {
+                    Map<String, String> fileInfo = new HashMap<>();
+                    fileInfo.put("fileName", file.getName());
+                    fileInfo.put("filePath", file.getAbsolutePath());
+                    fileList.add(fileInfo);
+                }
+            }
+        }
+    }
+
+    public void setExportCompleted(boolean value) {
+        lock.lock();
+        System.err.println(lock);
+        try {
+            exportCompleted = value;
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    private void deleteFilesInDirectory(String directoryPath) {
+        File directory = new File(directoryPath);
+        if (directory.exists() && directory.isDirectory()) {
+            File[] files = directory.listFiles();
+            if (files != null) {
+                for (File file : files) {
+                    file.delete();
+                }
+            }
+        }
+    }
+}

+ 1 - 2
zkqy-admin/src/main/java/com/zkqy/web/controller/projcetzip/ExportController.java

@@ -4,7 +4,6 @@ import com.zkqy.common.config.ZkqyConfig;
 import com.zkqy.common.core.domain.AjaxResult;
 import com.zkqy.common.core.domain.entity.SysUser;
 import com.zkqy.common.utils.SecurityUtils;
-import com.zkqy.web.controller.tool.DownloadController;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 import org.springframework.web.bind.annotation.PostMapping;
@@ -99,7 +98,7 @@ public class ExportController {
         } catch (IOException | InterruptedException | TimeoutException e) {
             e.printStackTrace();
         }
-    }
+     }
 
     private List<String> buildMysqldumpCommand(String databaseName, DataSource dataSource) {
         // 使用 Arrays.asList 创建不可变 List

+ 0 - 106
zkqy-admin/src/main/java/com/zkqy/web/controller/tool/DownloadController.java

@@ -1,106 +0,0 @@
-package com.zkqy.web.controller.tool;
-
-import com.zkqy.common.config.ZkqyConfig;
-import com.zkqy.common.utils.ZipUtils;
-import com.zkqy.system.service.ISysEngineeringService;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.http.HttpStatus;
-import org.springframework.http.ResponseEntity;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RestController;
-
-import javax.servlet.http.HttpServletResponse;
-import java.io.File;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.concurrent.locks.ReentrantLock;
-
-@RestController
-@RequestMapping("/download/project-db")
-public class DownloadController {
-
-    @Autowired
-    private ISysEngineeringService sysEngineeringService;
-
-    // 使用ReentrantLock确保多线程访问时的同步性
-    private static final ReentrantLock lock = new ReentrantLock();
-    private boolean exportCompleted = false;
-
-
-    @GetMapping("/status")
-    public ResponseEntity<String> checkExportStatus() {
-        lock.lock();
-        try {
-            if (exportCompleted) {
-                return ResponseEntity.ok("导出已完成。您现在可以触发下载");
-            } else {
-                return ResponseEntity.status(HttpStatus.NOT_FOUND).body("出口仍在进行中。请稍候。。。");
-            }
-        } finally {
-            lock.unlock();
-        }
-    }
-
-    @GetMapping("/download")
-    public void downloadZip(HttpServletResponse response) {
-        String sourceFilePath = ZkqyConfig.getUploadPath() + "/engineeringdownload/";
-        lock.lock();
-        try {
-            // 检查导出是否完成
-            if (exportCompleted) {
-                try {
-                    // 获取文件列表等
-                    List<Map<String, String>> fileList = listFiles(sourceFilePath);
-                    ZipUtils.createZip(response, fileList);
-                } catch (Exception e) {
-                    e.printStackTrace();
-                    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
-                }
-            } else {
-                // 如果导出未完成,可以返回相应的错误信息给前端
-                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
-                // 或者抛出异常等处理方式
-            }
-        } finally {
-            lock.unlock();
-        }
-    }
-
-    private List<Map<String, String>> listFiles(String directoryPath) {
-        List<Map<String, String>> fileList = new ArrayList<>();
-        File directory = new File(directoryPath);
-        if (directory.exists() && directory.isDirectory()) {
-            listFilesRecursively(directory, fileList);
-        }
-        return fileList;
-    }
-
-    private static void listFilesRecursively(File directory, List<Map<String, String>> fileList) {
-        File[] files = directory.listFiles();
-        if (files != null) {
-            for (File file : files) {
-                if (file.isFile()) {
-                    Map<String, String> fileMap = new HashMap<>();
-                    fileMap.put("fileName", file.getName());
-                    fileMap.put("filePath", file.getAbsolutePath());
-                    fileList.add(fileMap);
-                } else if (file.isDirectory()) {
-                    listFilesRecursively(file, fileList);
-                }
-            }
-        }
-    }
-
-    // 设置导出状态的方法,确保在同步块内进行
-    public void setExportCompleted(boolean value) {
-        lock.lock();
-        try {
-            exportCompleted = value;
-        } finally {
-            lock.unlock();
-        }
-    }
-}

+ 0 - 73
zkqy-common/src/main/java/com/zkqy/common/utils/ZipUtils.java

@@ -5,11 +5,9 @@ import org.apache.tools.zip.ZipEntry;
 import org.apache.tools.zip.ZipOutputStream;
 
 import javax.servlet.http.HttpServletResponse;
-import java.io.BufferedInputStream;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStream;
-import java.net.URL;
 import java.util.List;
 import java.util.Map;
 
@@ -22,25 +20,19 @@ public class ZipUtils {
      * @param fileList 文件信息集合
      */
     public static void createZip(HttpServletResponse response, List<Map<String, String>> fileList) {
-
         try {
             response.setContentType("application/x-octet-stream");
             response.setHeader("Content-Disposition", "attachment; filename=\"download.zip\"");
             response.setCharacterEncoding("utf-8");
-
             ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
             byte[] buffer = new byte[1024];
-
             for (Map<String, String> fileMap : fileList) {
                 String fileName = fileMap.get("fileName");
                 String filePath = fileMap.get("filePath");
-
                 // 使用 File.separator 来实现跨平台兼容性
                 String relativePath = fileName;
-
                 // 在 Zip 中创建带有相对路径的 ZipEntry
                 zos.putNextEntry(new ZipEntry(relativePath));
-
                 try (InputStream is = new FileInputStream(filePath)) {
                     int length;
                     while ((length = is.read(buffer)) > 0) {
@@ -49,77 +41,12 @@ public class ZipUtils {
                 } catch (IOException e) {
                     e.printStackTrace();
                 }
-
                 zos.closeEntry();
             }
-
             zos.close();
             zos.flush();
         } catch (IOException e) {
             e.printStackTrace();
         }
     }
-
-    // 222
-//    public static void createZip(HttpServletResponse response, List<Map<String, String>> fileList) {
-//        try (ZipOutputStream zos = new ZipOutputStream(response.getOutputStream())) {
-//            response.setContentType("application/x-octet-stream");
-//            response.setHeader("Content-Disposition", "attachment; filename=\"download.zip\"");
-//            response.setCharacterEncoding("utf-8");
-//
-//            byte[] buffer = new byte[1024];
-//            for (Map<String, String> file : fileList) {
-//                // 设置zip里面每个文件的名称
-//                ZipEntry entry = new ZipEntry(file.get("fileName").toString());
-//                zos.putNextEntry(entry);
-//
-//                try (InputStream is = new FileInputStream(file.get("filePath").toString())) {
-//                    int length;
-//                    while ((length = is.read(buffer)) > 0) {
-//                        zos.write(buffer, 0, length);
-//                    }
-//                }
-//
-//                zos.closeEntry();
-//            }
-//
-//            zos.flush();
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//        }
-//    }
-
-    // 111
-//    public static void createZip(HttpServletResponse response, List<Map<String, String>> fileList) {
-//
-//        try {
-//            //设置下载的文件名称, 注意中文名需要做编码类型转换
-//            response.setContentType("application/x-octet-stream");
-//            response.setHeader("Content-Disposition", "attachment;");
-//            response.setCharacterEncoding("utf-8");
-//            //创建zip输出流
-//            ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
-//            byte[] buffer = new byte[1024];
-//            BufferedInputStream bufferStream = null;
-//            int index = 1;
-//            for (int i = 0; i < fileList.size(); i++) {
-//                //设置zip里面每个文件的名称
-//                zos.putNextEntry(new ZipEntry(fileList.get(i).get("fileName").toString()));
-//                //根据文件地址获取输入流
-//                InputStream is = new URL("file:///" + fileList.get(i).get("filePath").toString()).openConnection().getInputStream();
-//                int length;
-//                while ((length = is.read(buffer)) > 0) {
-//                    zos.write(buffer, 0, length);
-//                }
-//                is.close();
-//                index++;
-//            }
-//            zos.closeEntry();
-//            zos.close();
-//            zos.flush();
-//        } catch (IOException e) {
-//            e.printStackTrace();
-//        }
-//
-//    }
 }