|
@@ -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();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|