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