From 9552ea3618897507bc00910e170019ac8867423b Mon Sep 17 00:00:00 2001
From: zouyu <2723363702@qq.com>
Date: 星期三, 26 十一月 2025 17:51:01 +0800
Subject: [PATCH] 合同编号识别调整:识别失败文件也输出到目标目录

---
 src/main/java/com/xindao/ocr/swingui/swing/jpanel/ContractNumberProcessPanel.java |   33 +++++++++++++++++++++++++--------
 1 files changed, 25 insertions(+), 8 deletions(-)

diff --git a/src/main/java/com/xindao/ocr/swingui/swing/jpanel/ContractNumberProcessPanel.java b/src/main/java/com/xindao/ocr/swingui/swing/jpanel/ContractNumberProcessPanel.java
index 112b9bd..77d7509 100644
--- a/src/main/java/com/xindao/ocr/swingui/swing/jpanel/ContractNumberProcessPanel.java
+++ b/src/main/java/com/xindao/ocr/swingui/swing/jpanel/ContractNumberProcessPanel.java
@@ -12,6 +12,7 @@
 import org.apache.commons.lang3.StringUtils;
 import org.apache.pdfbox.pdmodel.PDDocument;
 import org.apache.pdfbox.rendering.PDFRenderer;
+import org.apache.pdfbox.util.filetypedetector.FileType;
 import org.apache.poi.util.IOUtils;
 
 import javax.swing.*;
@@ -281,6 +282,21 @@
         }
     }
 
+    /**
+     * 杈撳嚭鏂囦欢
+     * @param newFileName 鏂版枃浠跺悕
+     * @param file 婧愭枃浠�
+     * @param fileSuffix 鏂囦欢鍚庣紑
+     * @param targetPath 鐩爣璺緞
+     */
+    private void writeFile(String newFileName,File file,String fileSuffix,File targetPath) throws IOException {
+        String outputFileName = newFileName + fileSuffix;
+        File outputFile = new File(targetPath, outputFileName);
+        if (!outputFile.getParentFile().exists()) {
+            outputFile.getParentFile().mkdirs();
+        }
+        IOUtils.copy(Files.newInputStream(file.toPath()),outputFile);
+    }
 
     /**
      * 澶勭悊鍚堝悓缂栧彿鏂规硶
@@ -307,14 +323,14 @@
 
             for (File file : selectedFiles) {
                 processCount++;
+                //鑾峰彇璇嗗埆鍒扮殑绗竴涓唴瀹�
+                String text = file.getName().replace(".pdf","");
                 try {
                     //鎴彇pdf閫夊尯鍥惧儚
                     String pathStr = capturePdfArea(file, prefs);
 //                    ToFile.preprocessImage(pathStr);
                     //璇诲彇鍥惧儚鍐呭
                     String ocrFullText = FileNameValidator.validateAndCleanFileName(ocrService.ocr(pathStr.replaceFirst("/", "")));
-                    //鑾峰彇璇嗗埆鍒扮殑绗竴涓唴瀹�
-                    String text = file.getName().replace(".pdf","");
                     if(StringUtils.isNotBlank(ocrFullText) && !StringUtils.equals(ocrFullText,text)){
                         text = ocrFullText;
                         String finalText = text;
@@ -324,12 +340,7 @@
                             fileIndex.getAndIncrement();
                         }
                         //灏嗚瘑鍒殑鍐呭璁剧疆涓烘枃浠跺悕锛屽鍑哄埌鎸囧畾鐩綍
-                        String outputFileName = text + ".pdf";
-                        File outputFile = new File(outputDirectory, outputFileName);
-                        if (!outputFile.getParentFile().exists()) {
-                            outputFile.getParentFile().mkdirs();
-                        }
-                        IOUtils.copy(Files.newInputStream(file.toPath()),outputFile);
+                        writeFile(text,file, ".pdf",outputDirectory);
                     }
                     successCount++;
                     contractNumberList.add(new ContractNumberExcelData(text));
@@ -338,6 +349,12 @@
                     failCount++;
                     e.printStackTrace();
                     log("澶勭悊澶辫触: " + file.getName() + " - " + e.getMessage());
+                    //澶勭悊澶辫触鐨勬枃浠朵篃杈撳嚭
+                    try {
+                        writeFile(text+"_fail", file, ".pdf",outputDirectory);
+                    } catch (IOException ex) {
+                        throw new RuntimeException(ex);
+                    }
                 }finally {
                     //鍒犻櫎涓存椂鐩綍
                     ToFile.deleteTempFiles(OcrSwingConstants.cacheDir);

--
Gitblit v1.9.3