Selaa lähdekoodia

fix:上传文件解析时的异常问题

wangzaijun 1 viikko sitten
vanhempi
commit
928d65a0fe

+ 13 - 6
mo-daq/src/main/java/com/smppw/modaq/application/service/EmailParseApiServiceImpl.java

@@ -24,6 +24,7 @@ import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
 import java.nio.file.Files;
+import java.nio.file.Path;
 import java.util.Date;
 import java.util.List;
 
@@ -106,15 +107,21 @@ public class EmailParseApiServiceImpl implements EmailParseApiService {
         for (MultipartFile file : files) {
             String filename = file.getOriginalFilename();
             File saveFile = this.emailParseService.generateSavePath("upload", new Date(), filename);
-            String filepath = saveFile.getPath();
-            try (InputStream is = file.getInputStream()) {
-                Files.copy(is, saveFile.toPath());
-            } catch (IOException e) {
-                log.warn("文件{} 上传失败:{}", filename, ExceptionUtil.stacktraceToString(e));
-                dataList.add(new UploadReportResult(filepath, ReportParseStatus.FILE_UPLOAD_FAIL));
+            String filepath = saveFile.getAbsolutePath();
+            if (!saveFile.exists()) {
+                if (!saveFile.getParentFile().exists()) {
+                    saveFile.getParentFile().mkdirs();
+                }
+                try (InputStream is = file.getInputStream()) {
+                    Files.copy(is, Path.of(filepath));
+                } catch (IOException e) {
+                    log.warn("文件{} 上传失败:{}", filename, ExceptionUtil.stacktraceToString(e));
+                    dataList.add(new UploadReportResult(filepath, ReportParseStatus.FILE_UPLOAD_FAIL));
+                }
             }
             params.getReportInfos().add(new UploadReportParams.ReportInfo(filepath));
         }
+        params.setTitle("报告上传解析");
         List<UploadReportResult> tempList = this.emailParseService.uploadReportResults(params);
         dataList.addAll(tempList);
         return dataList;

+ 28 - 16
mo-daq/src/main/java/com/smppw/modaq/domain/service/EmailParseService.java

@@ -171,19 +171,7 @@ public class EmailParseService {
                 }
             }
             // 重新判断类型
-            for (EmailZipFileDTO emailFile : emailFileList) {
-                if (EmailTypeConst.SUPPORT_NO_OTHER_TYPES.contains(emailFile.getEmailType())) {
-                    continue;
-                }
-                Integer type = EmailUtil.getEmailTypeBySubject(emailTitle + emailFile.getFilename());
-                // 特殊月报
-                if ((Objects.equals(EmailTypeConst.NAV_EMAIL_TYPE, type) || Objects.equals(EmailTypeConst.REPORT_OTHER_TYPE, type))
-                        && ReportParseUtils.containsAny(emailTitle, ReportParseUtils.MONTHLY_REPORT_KEYWORDS)) {
-                    type = EmailTypeConst.REPORT_EMAIL_TYPE;
-                }
-                emailFile.setEmailType(type);
-            }
-
+            this.recheckEmailType(emailTitle, emailFileList);
             Iterator<EmailZipFileDTO> entryIterator = emailFileList.iterator();
             while (entryIterator.hasNext()) {
                 EmailZipFileDTO entry = entryIterator.next();
@@ -334,13 +322,14 @@ public class EmailParseService {
      */
     public List<UploadReportResult> uploadReportResults(UploadReportParams params) {
         List<ParseResult<ReportData>> dataList = ListUtil.list(false);
+        String emailTitle = params.getTitle();
         List<UploadReportParams.ReportInfo> reportInfos = params.getReportInfos();
         List<EmailZipFileDTO> dtos = ListUtil.list(false);
         for (UploadReportParams.ReportInfo e : reportInfos) {
             String reportPath = e.getReportPath();
             if (ArchiveUtil.isArchive(reportPath)) {
                 try {
-                    this.handleCompressedFiles(params.getTitle(), reportPath, e.getReportType(), dtos);
+                    this.handleCompressedFiles(emailTitle, reportPath, e.getReportType(), dtos);
                 } catch (Exception ex) {
                     log.warn("报告{} 压缩包解压失败:{}", reportPath, ExceptionUtil.stacktraceToString(ex));
                     ReportData reportData = new ReportData.DefaultReportData();
@@ -348,10 +337,12 @@ public class EmailParseService {
                     dataList.add(new ParseResult<>(ReportParseStatus.ARCHIVE_FAIL, reportData));
                 }
             } else {
-                dtos.add(new EmailZipFileDTO(params.getTitle(), reportPath, e.getReportType()));
+                dtos.add(new EmailZipFileDTO(emailTitle, reportPath, e.getReportType()));
             }
         }
-        EmailInfoDTO emailInfo = new EmailInfoDTO(params.getTitle(), dtos);
+        // 重新判断类型
+        this.recheckEmailType(emailTitle, dtos);
+        EmailInfoDTO emailInfo = new EmailInfoDTO(emailTitle, dtos);
         // 附件文件检查
         Long totalSize = this.checkEmailFileInfo(emailInfo);
         if (totalSize == null) {
@@ -367,6 +358,27 @@ public class EmailParseService {
     }
 
     /**
+     * 重新校验邮件附件的类型(用邮件主题+附件名称)
+     *
+     * @param emailTitle 邮件主题
+     * @param dtos       所有附件
+     */
+    private void recheckEmailType(String emailTitle, List<EmailZipFileDTO> dtos) {
+        for (EmailZipFileDTO emailFile : dtos) {
+            if (EmailTypeConst.SUPPORT_NO_OTHER_TYPES.contains(emailFile.getEmailType())) {
+                continue;
+            }
+            Integer type = EmailUtil.getEmailTypeBySubject(emailTitle + emailFile.getFilename());
+            // 特殊月报
+            if ((Objects.equals(EmailTypeConst.NAV_EMAIL_TYPE, type) || Objects.equals(EmailTypeConst.REPORT_OTHER_TYPE, type))
+                    && ReportParseUtils.containsAny(emailTitle, ReportParseUtils.MONTHLY_REPORT_KEYWORDS)) {
+                type = EmailTypeConst.REPORT_EMAIL_TYPE;
+            }
+            emailFile.setEmailType(type);
+        }
+    }
+
+    /**
      * 邮件信息前置处理,在解析操作执行之前的过滤逻辑和校验逻辑。返回所有附件大小汇总
      *
      * @param emailInfo 邮件信息(包含所有解压后的文件)