|
@@ -1,6 +1,7 @@
|
|
|
package com.zkqy.web.controller.system;
|
|
|
|
|
|
import java.io.*;
|
|
|
+import java.net.URLEncoder;
|
|
|
import java.nio.file.DirectoryStream;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
@@ -8,7 +9,11 @@ import java.nio.file.Paths;
|
|
|
import java.util.*;
|
|
|
import java.util.zip.ZipEntry;
|
|
|
import java.util.zip.ZipOutputStream;
|
|
|
+import javax.servlet.ServletOutputStream;
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
+import com.zkqy.common.annotation.Anonymous;
|
|
|
import com.zkqy.common.config.ZkqyConfig;
|
|
|
import com.zkqy.common.core.domain.entity.DataSource;
|
|
|
import com.zkqy.common.core.domain.entity.SysUser;
|
|
@@ -16,19 +21,14 @@ import com.zkqy.common.utils.DateUtils;
|
|
|
import com.zkqy.common.utils.SecurityUtils;
|
|
|
import com.zkqy.common.utils.ZipUtils;
|
|
|
import io.swagger.annotations.ApiOperation;
|
|
|
+import org.springframework.core.io.FileSystemResource;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
import org.springframework.security.access.prepost.PreAuthorize;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
-import org.springframework.web.bind.annotation.GetMapping;
|
|
|
-import org.springframework.web.bind.annotation.PostMapping;
|
|
|
-import org.springframework.web.bind.annotation.PutMapping;
|
|
|
-import org.springframework.web.bind.annotation.DeleteMapping;
|
|
|
-import org.springframework.web.bind.annotation.PathVariable;
|
|
|
-import org.springframework.web.bind.annotation.RequestBody;
|
|
|
-import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
-import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
import com.zkqy.common.annotation.Log;
|
|
|
import com.zkqy.common.core.controller.BaseController;
|
|
|
import com.zkqy.common.core.domain.AjaxResult;
|
|
@@ -119,12 +119,39 @@ public class SysEngineeringController extends BaseController {
|
|
|
return toAjax(sysEngineeringService.deleteSysEngineeringByIds(ids));
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ @GetMapping("/largeFile")
|
|
|
+ public ResponseEntity<Resource> downloadLargeFile() {
|
|
|
+ // 调用Service层获取文件
|
|
|
+ File name = new File("/Users/zrwj/Downloads/123.zip");
|
|
|
+ // 调用 FileDownloadService 中的方法获取文件
|
|
|
+ if (Objects.isNull(name)) {
|
|
|
+ throw new RuntimeException("文件名称不能为空");
|
|
|
+ }
|
|
|
+ // directoryPath为服务器文件路径
|
|
|
+ String path = "" + name;
|
|
|
+ File file = new File(path);
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
+ headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
|
|
|
+ headers.add("Content-Disposition", "attachment; filename=" + file.getName());
|
|
|
+ headers.add("Pragma", "no-cache");
|
|
|
+ headers.add("Expires", "0");
|
|
|
+ headers.add("Last-Modified", new Date().toString());
|
|
|
+ headers.add("ETag", String.valueOf(System.currentTimeMillis()));
|
|
|
+ return ResponseEntity
|
|
|
+ .ok()
|
|
|
+ .headers(headers)
|
|
|
+ .contentLength(file.length())
|
|
|
+ .contentType(MediaType.parseMediaType("application/octet-stream"))
|
|
|
+ .body(new FileSystemResource(file));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 下载压缩包
|
|
|
*/
|
|
|
@GetMapping("/download")
|
|
|
public ResponseEntity<StreamingResponseBody> downloadZip() {
|
|
|
- System.err.println("开始调用" + DateUtils.getTime());
|
|
|
// 指定要打包的文件或目录路径
|
|
|
String sourceFilePath = ZkqyConfig.getUploadPath() + "/engineeringdownload/";
|
|
|
|
|
@@ -136,19 +163,16 @@ public class SysEngineeringController extends BaseController {
|
|
|
System.out.println("目录 '" + sourceFilePath + "' 创建失败.");
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
//当前用户数据源信息
|
|
|
DataSource datasourceInfo = SecurityUtils.getDatasourceInfo();
|
|
|
//导出数据库集合
|
|
|
Set<String> databaseList = new LinkedHashSet<>();
|
|
|
databaseList.add("ry-vue-call");
|
|
|
-// databaseList.add("zkqy-template");
|
|
|
+ // databaseList.add("zkqy-template");
|
|
|
// 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,35 +192,23 @@ 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);
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/**
|
|
|
* 下载压缩包
|
|
|
*/
|
|
|
+
|
|
|
+ @Anonymous
|
|
|
@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()) {
|
|
|
- boolean isCreated = dir.mkdirs();
|
|
|
- if (!isCreated) {
|
|
|
- System.out.println("目录 '" + sourceFilePath + "' 创建失败.");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
//当前用户数据源信息
|
|
|
DataSource datasourceInfo = SecurityUtils.getDatasourceInfo();
|
|
|
//导出数据库集合
|
|
@@ -204,10 +216,7 @@ 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();
|
|
|
SysEngineering sysEngineering = new SysEngineering();
|
|
@@ -216,12 +225,12 @@ 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 {
|
|
|
+
|
|
|
+ private void addFilesToZipRecursively(ZipOutputStream zipOut, Path rootPath, DirectoryStream<Path> dirStream) throws
|
|
|
+ IOException {
|
|
|
for (Path filePath : dirStream) {
|
|
|
if (Files.isDirectory(filePath)) {
|
|
|
// 添加目录条目到ZIP
|
|
@@ -254,74 +263,49 @@ public class SysEngineeringController extends BaseController {
|
|
|
/**
|
|
|
* 导出数据库文件
|
|
|
*/
|
|
|
- public void exportMultipleDatabasesToLocal(Set<String> databaseNames, String outputPath, DataSource dataSource) {
|
|
|
- outputPath = outputPath + "sql/";
|
|
|
+ private static final String MYSQLDUMP_PATH = "/usr/local/mysql/bin/mysqldump";
|
|
|
+// private static final String OUTPUT_PATH = "sql/";
|
|
|
|
|
|
- File dir = new File(outputPath);
|
|
|
- // 判断文件夹路径是否存在,不存在创建
|
|
|
- if (!dir.exists()) {
|
|
|
- boolean isCreated = dir.mkdirs();
|
|
|
- if (!isCreated) {
|
|
|
- System.out.println("目录 '" + outputPath + "' 创建失败.");
|
|
|
- }
|
|
|
- }
|
|
|
- for (String databaseName : databaseNames) {
|
|
|
- try {
|
|
|
- // 构建命令参数列表
|
|
|
- List<String> cmd = new ArrayList<>();
|
|
|
-// 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");
|
|
|
- cmd.add(dataSource.getUsername());
|
|
|
- cmd.add("-p" + dataSource.getPassword());
|
|
|
- cmd.add(databaseName);
|
|
|
+ public void exportMultipleDatabasesToLocal(Set<String> databaseNames, String sourceFilePath, DataSource dataSource) {
|
|
|
|
|
|
- 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");
|
|
|
+ String outputPath = sourceFilePath + "sql/" + SecurityUtils.getTenantId();
|
|
|
|
|
|
- // 创建进程并获取输出流
|
|
|
- Process process = pb.start();
|
|
|
+ try {
|
|
|
+ Files.createDirectories(Paths.get(outputPath));
|
|
|
|
|
|
- // 将mysqldump的输出重定向到文件
|
|
|
- OutputStream outStream = new FileOutputStream(Paths.get(outputPath + databaseName + ".sql").toFile());
|
|
|
- InputStream processOutput = process.getInputStream();
|
|
|
- byte[] buffer = new byte[4096];
|
|
|
- int read;
|
|
|
- while ((read = processOutput.read(buffer)) != -1) {
|
|
|
- outStream.write(buffer, 0, read);
|
|
|
- }
|
|
|
- outStream.close();
|
|
|
- processOutput.close();
|
|
|
-
|
|
|
- // 获取错误流,并打印或处理任何错误信息
|
|
|
- InputStream errorStream = process.getErrorStream();
|
|
|
- BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
|
|
|
- String line;
|
|
|
- while ((line = errorReader.readLine()) != null) {
|
|
|
- System.err.println(line);
|
|
|
- }
|
|
|
- errorReader.close();
|
|
|
- errorStream.close();
|
|
|
+ for (String databaseName : databaseNames) {
|
|
|
+ String dumpFileName = outputPath + "/" + databaseName + ".sql";
|
|
|
+ List<String> cmd = buildMysqldumpCommand(databaseName, dataSource);
|
|
|
+
|
|
|
+ ProcessBuilder pb = new ProcessBuilder(cmd);
|
|
|
+ pb.redirectOutput(new File(dumpFileName));
|
|
|
|
|
|
- // 等待进程结束并检查退出码
|
|
|
+ Process process = pb.start();
|
|
|
int exitCode = process.waitFor();
|
|
|
+
|
|
|
if (exitCode == 0) {
|
|
|
- System.out.println("数据库备份成功,导出文件: " + outputPath);
|
|
|
+ System.out.println("数据库备份成功,导出文件: " + dumpFileName);
|
|
|
} else {
|
|
|
System.err.println("mysqldump命令执行失败,退出代码: " + exitCode);
|
|
|
}
|
|
|
-
|
|
|
- } catch (IOException | InterruptedException e) {
|
|
|
- e.printStackTrace();
|
|
|
}
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private List<String> buildMysqldumpCommand(String databaseName, DataSource dataSource) {
|
|
|
+ List<String> cmd = new ArrayList<>();
|
|
|
+ cmd.add(MYSQLDUMP_PATH);
|
|
|
+ cmd.add("-h");
|
|
|
+ cmd.add(dataSource.getDatabaseIp());
|
|
|
+ cmd.add("-u");
|
|
|
+ cmd.add(dataSource.getUsername());
|
|
|
+ cmd.add("-p" + dataSource.getPassword());
|
|
|
+ cmd.add(databaseName);
|
|
|
+ return cmd;
|
|
|
+ }
|
|
|
+
|
|
|
public static List<Map<String, String>> listFiles(String directoryPath) {
|
|
|
List<Map<String, String>> fileList = new ArrayList<>();
|
|
|
File directory = new File(directoryPath);
|
|
@@ -350,12 +334,5 @@ public class SysEngineeringController extends BaseController {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args) {
|
|
|
- String directoryPath = "/path/to/your/directory"; // 替换为你的文件夹路径
|
|
|
- List<Map<String, String>> fileList = listFiles(directoryPath);
|
|
|
|
|
|
- for (Map<String, String> fileMap : fileList) {
|
|
|
- System.out.println("FileName: " + fileMap.get("fileName") + ", FilePath: " + fileMap.get("filePath"));
|
|
|
- }
|
|
|
- }
|
|
|
}
|