From 04ada8740a237c15339cf6d24cf75b12685bba3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BE=E5=90=8C=E5=AD=A6?= Date: Thu, 23 Oct 2025 13:18:54 +0800 Subject: [PATCH] =?UTF-8?q?UPDATE:=20=E4=BF=AE=E6=94=B9=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=90=88=E5=B9=B6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plan/controller/AdPlanController.java | 14 ++++++++- .../gather/plan/service/AsyncPlanHandler.java | 31 +++++++------------ 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java b/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java index a4313b16..2602d146 100644 --- a/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java +++ b/detection/src/main/java/com/njcn/gather/plan/controller/AdPlanController.java @@ -42,6 +42,8 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; +import java.io.File; +import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -452,7 +454,17 @@ public class AdPlanController extends BaseController { fileTypeError.setMessage("请上传zip文件"); throw new BusinessException(fileTypeError); } - asyncPlanHandler.importAndMergePlanCheckData(file, getUserId(), planId); + // 创建临时文件 + File tempFile = cn.hutool.core.io.FileUtil.createTempFile(); + // 将MultipartFile内容写入临时文件 + try { + file.transferTo(tempFile); + } catch (IOException e) { + throw new BusinessException(CommonResponseEnum.FAIL, "文件保存失败"); + } + // 获取文件路径 + String filePath = tempFile.getAbsolutePath(); + asyncPlanHandler.importAndMergePlanCheckData(filePath, getUserId(), planId); return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, true, methodDescribe); } diff --git a/detection/src/main/java/com/njcn/gather/plan/service/AsyncPlanHandler.java b/detection/src/main/java/com/njcn/gather/plan/service/AsyncPlanHandler.java index e4eac3d3..cdf67c5c 100644 --- a/detection/src/main/java/com/njcn/gather/plan/service/AsyncPlanHandler.java +++ b/detection/src/main/java/com/njcn/gather/plan/service/AsyncPlanHandler.java @@ -37,10 +37,8 @@ import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.EnableAsync; import org.springframework.stereotype.Component; import org.springframework.transaction.annotation.Transactional; -import org.springframework.web.multipart.MultipartFile; import java.io.File; -import java.io.IOException; import java.time.Duration; import java.time.LocalDateTime; import java.util.ArrayList; @@ -65,6 +63,8 @@ public class AsyncPlanHandler { private final IAdPariService adPairService; private final JdbcTemplate jdbcTemplate; + + @Value("${report.reportDir}") private String reportPath; @Value("${data.homeDir}") @@ -235,35 +235,28 @@ public class AsyncPlanHandler { } - @Transactional @Async - public void importAndMergePlanCheckData(MultipartFile file, String uid, String planId) { + public void importAndMergePlanCheckData(String zipFilePath, String uid, String planId) { + importAndMergePlanCheckDataLogic(zipFilePath, uid, planId); + + } + + @Transactional + public void importAndMergePlanCheckDataLogic(String zipFilePath, String uid, String planId) { NonWebAutoFillValueHandler.setCurrentUserId(uid); LocalDateTime startTime = LocalDateTime.now(); AtomicInteger progress = new AtomicInteger(0); AtomicInteger currentProgress = new AtomicInteger(0); AtomicInteger dataCount = new AtomicInteger(0); try { - sseClient.sendMessage(uid, planId, HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), progress, "开始保存文件,请耐心等待...")); // 创建临时目录用于解压文件 File tempDir = FileUtil.mkdir(FileUtil.getTmpDirPath() + "import_plan_check_data_" + System.currentTimeMillis() + "/"); - - // 将上传的zip文件保存到临时目录 - File zipFile = FileUtil.file(tempDir, file.getOriginalFilename()); - try { - // 直接获取文件字节,避免依赖Tomcat临时文件 - byte[] fileBytes = file.getBytes(); - FileUtil.writeBytes(fileBytes, zipFile); - } catch (IOException e) { - log.error("保存上传文件失败", e); - throw e; - } progress.addAndGet(1); sseClient.sendMessage(uid, planId, HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), progress, "开始解压文件,请耐心等待...")); // 解压zip文件 File unzipDir = FileUtil.mkdir(FileUtil.file(tempDir, "unzip")); - ZipUtil.unzip(zipFile.getAbsolutePath(), unzipDir.getAbsolutePath()); + ZipUtil.unzip(zipFilePath, unzipDir.getAbsolutePath()); // 查找解压目录中的json文件 File[] files = unzipDir.listFiles(); @@ -464,14 +457,14 @@ public class AsyncPlanHandler { sseClient.sendMessage(uid, planId, HttpResultUtil.assembleResult(CommonResponseEnum.SUCCESS.getCode(), progress, "数据合并完成")); } catch (Exception e) { + log.error("导入数据失败", e); sseClient.sendMessage(uid, planId, HttpResultUtil.assembleResult(CommonResponseEnum.FAIL.getCode(), progress.get() + currentProgress.get(), "导入失败")); } finally { NonWebAutoFillValueHandler.clearCurrentUserId(); } - + FileUtil.del(zipFilePath); sseClient.closeSse(uid); - } // 构建分页查询SQL