فهرست منبع

feat:优化工程文件下载导出时间过长问题性能提升(35%)

韩帛霖 1 سال پیش
والد
کامیت
fc5d8e2e71

+ 16 - 10
zkqy-admin/src/main/java/com/zkqy/web/controller/system/SysEngineeringController.java

@@ -125,7 +125,7 @@ public class SysEngineeringController extends BaseController {
      */
     @GetMapping("/download")
     public ResponseEntity<StreamingResponseBody> downloadZip() {
-
+        System.err.println("开始调用" + DateUtils.getTime());
         // 指定要打包的文件或目录路径
         String sourceFilePath = ZkqyConfig.getUploadPath() + "/engineeringdownload/";
 
@@ -147,8 +147,9 @@ public class SysEngineeringController extends BaseController {
 //        databaseList.add("zkqy-call");
         databaseList.add(datasourceInfo.getDatabaseName());
         //将sql文件导出到指定目录下
+        System.err.println("SQL文件开始生成" + DateUtils.getTime());
         exportMultipleDatabasesToLocal(databaseList, sourceFilePath, datasourceInfo);
-
+        System.err.println("SQL文件生成完毕" + DateUtils.getTime());
         //下载成功插入数据
         SysUser sysUser = SecurityUtils.getLoginUser().getUser();
         SysEngineering sysEngineering = new SysEngineering();
@@ -168,14 +169,13 @@ public class SysEngineeringController extends BaseController {
                 .body(outputStream -> {
                     try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get(sourceFilePath));
                          ZipOutputStream zipOut = new ZipOutputStream(outputStream)) {
-
+                        System.err.println("开始导出文件" + DateUtils.getTime());
                         addFilesToZipRecursively(zipOut, Paths.get(sourceFilePath), directoryStream);
-
+                        System.err.println("成功导出文件" + DateUtils.getTime());
                     } catch (IOException e) {
                         throw new RuntimeException("Failed to create the ZIP stream.", e);
                     }
                 });
-
     }
 
     /**
@@ -183,10 +183,11 @@ public class SysEngineeringController extends BaseController {
      */
     @GetMapping("/download1")
     public void downloadZip1(HttpServletResponse response) {
+        System.err.println("开始调用" + DateUtils.getTime());
+
         // 指定要打包的文件或目录路径
         String sourceFilePath = ZkqyConfig.getUploadPath() + "/engineeringdownload/";
 
-
         File dir = new File(sourceFilePath);
         // 判断文件夹路径是否存在,不存在创建
         if (!dir.exists()) {
@@ -204,7 +205,9 @@ public class SysEngineeringController extends BaseController {
         databaseList.add("ry-vue-call");
         databaseList.add(datasourceInfo.getDatabaseName());
         //将sql文件导出到指定目录下
+        System.err.println("SQL文件开始生成" + DateUtils.getTime());
         exportMultipleDatabasesToLocal(databaseList, sourceFilePath, datasourceInfo);
+        System.err.println("SQL文件生成完毕" + DateUtils.getTime());
 
         //下载成功插入数据
         SysUser sysUser = SecurityUtils.getLoginUser().getUser();
@@ -214,7 +217,9 @@ public class SysEngineeringController extends BaseController {
         sysEngineering.setEngineeringName(sysUser.getTenantName() + "-mes");
         sysEngineeringService.insertSysEngineering(sysEngineering);
         List<Map<String, String>> fileList = listFiles(sourceFilePath);
+        System.err.println("开始导出文件" + DateUtils.getTime());
         ZipUtils.createZip(response, fileList);
+        System.err.println("成功导出文件" + DateUtils.getTime());
     }
 
     private void addFilesToZipRecursively(ZipOutputStream zipOut, Path rootPath, DirectoryStream<Path> dirStream) throws IOException {
@@ -265,8 +270,9 @@ public class SysEngineeringController extends BaseController {
             try {
                 // 构建命令参数列表
                 List<String> cmd = new ArrayList<>();
-                cmd.add("mysqldump");
-                cmd.add("--column-statistics=0");
+//                cmd.add("mysqldump");
+                cmd.add("/usr/local/mysql/bin/mysqldump");
+//                cmd.add("--column-statistics=0");
                 cmd.add("-h");
                 cmd.add(dataSource.getDatabaseIp());
                 cmd.add("-u");
@@ -276,8 +282,8 @@ public class SysEngineeringController extends BaseController {
 
                 ProcessBuilder pb = new ProcessBuilder(cmd);
                 // 设置MySQL bin目录到PATH环境变量,确保mysqldump可执行文件能找到
-                Map<String, String> env = pb.environment();
-                env.put("PATH", env.get("PATH") + File.pathSeparator + "\\usr\\local\\mysql\\bin");
+//                Map<String, String> env = pb.environment();
+//                env.put("PATH", env.get("PATH") + File.pathSeparator + "\\usr\\local\\mysql\\bin");
 
                 // 创建进程并获取输出流
                 Process process = pb.start();

+ 5 - 7
zkqy-common/src/main/java/com/zkqy/common/utils/file/ZipUtils.java

@@ -18,11 +18,9 @@ public class ZipUtils {
      * 创建多文件压缩包
      *
      * @param response
-     *
-     * @param fileList  文件信息集合
-     *
+     * @param fileList 文件信息集合
      */
-    public static void createZip(HttpServletResponse response, List<Map<String,String>> fileList ) {
+    public static void createZip(HttpServletResponse response, List<Map<String, String>> fileList) {
 
         try {
             //设置下载的文件名称, 注意中文名需要做编码类型转换
@@ -34,17 +32,17 @@ public class ZipUtils {
             byte[] buffer = new byte[1024];
             BufferedInputStream bufferStream = null;
             int index = 1;
-            for (int i = 0 ; i < fileList.size() ; i++) {
+            for (int i = 0; i < fileList.size(); i++) {
                 //设置zip里面每个文件的名称
                 zos.putNextEntry(new ZipEntry(fileList.get(i).get("fileName").toString()));
                 //根据文件地址获取输入流
-                InputStream is = new URL(fileList.get(i).get("url").toString()).openConnection().getInputStream();
+                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 ++;
+                index++;
             }
             zos.closeEntry();
             zos.close();