Sfoglia il codice sorgente

文件校对问题

devops 1 settimana fa
parent
commit
94264dfd35

+ 2 - 2
ruoyi-framework/pom.xml

@@ -21,12 +21,12 @@
         <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-web</artifactId>
-<!--            <exclusions>
+            <exclusions>
                 <exclusion>
                     <groupId>org.springframework.boot</groupId>
                     <artifactId>spring-boot-starter-tomcat</artifactId>
                 </exclusion>
-            </exclusions>-->
+            </exclusions>
         </dependency>
 
         <!-- SpringBoot 拦截器 -->

+ 2 - 5
ruoyi-project/src/main/java/com/ruoyi/project/controller/TrsFileInfoController.java

@@ -112,11 +112,8 @@ public class TrsFileInfoController extends BaseController
                 fileType = documentUtil.checkLegacyOfficeFile(file);
             }
 
-            // 提取文件内容
-            String content;
-            try (InputStream contentInputStream = file.getInputStream()) {
-                content = documentUtil.extractContent(fileType, contentInputStream);
-            }
+            // 使用智能提取方法
+            String content = documentUtil.smartExtractContent(file, fileType);
 
             TrsFileInfo trsFileInfo = new TrsFileInfo();
             Long deptId = getSysUser().getDeptId();

+ 51 - 2
ruoyi-project/src/main/java/com/ruoyi/project/utils/DocumentUtil.java

@@ -166,6 +166,53 @@ public class DocumentUtil {
         return content;
     }
 
+    /**
+     * 智能提取文件内容,优先根据扩展名判断
+     */
+    public String smartExtractContent(MultipartFile file, String fileType) throws IOException {
+        String fileName = file.getOriginalFilename();
+
+        // 优先根据扩展名判断
+        if (fileName != null) {
+            String lowerFileName = fileName.toLowerCase();
+
+            // 处理Excel文件
+            if (lowerFileName.endsWith(".xlsx")) {
+                try (InputStream inputStream = file.getInputStream()) {
+                    return extractXLSXContent(inputStream);
+                }
+            } else if (lowerFileName.endsWith(".xls")) {
+                // 如果是.xls文件,即使魔数识别为wps,也尝试按xls处理
+                try (InputStream inputStream = file.getInputStream()) {
+                    return extractXLSContent(inputStream);
+                } catch (Exception e) {
+                    // 如果按xls处理失败,且魔数识别为wps,则尝试按wps表格处理
+                    if ("wps".equals(fileType)) {
+                        try (InputStream inputStream = file.getInputStream()) {
+                            return extractWPSETContent(inputStream);
+                        }
+                    }
+                    throw e;
+                }
+            } else if (lowerFileName.endsWith(".et")) {
+                // WPS表格文件
+                try (InputStream inputStream = file.getInputStream()) {
+                    return extractWPSETContent(inputStream);
+                }
+            }
+
+            // 处理其他文件
+            try (InputStream inputStream = file.getInputStream()) {
+                return extractContent(fileType, inputStream);
+            }
+        }
+
+        // 如果没有扩展名,按原逻辑处理
+        try (InputStream inputStream = file.getInputStream()) {
+            return extractContent(fileType, inputStream);
+        }
+    }
+
     /**
      * 提取WPS文档内容(.wps, .wpt格式)
      */
@@ -485,7 +532,8 @@ public class DocumentUtil {
                 extractedText.append("\n");
             }
             //
-            return extractedText.toString().replaceAll("\\r?\\n", "</br>").replaceAll("\\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
+//            return extractedText.toString().replaceAll("\\r?\\n", "</br>").replaceAll("\\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
+            return extractedText.toString().replaceAll("\\r?\\n", "</br>").replaceAll("&nbsp;", "");
         }catch (Exception e){
             e.printStackTrace();
         }finally {
@@ -538,7 +586,8 @@ public class DocumentUtil {
                 }
                 extractedText.append("\n");
             }
-            return extractedText.toString().replaceAll("\\r?\\n", "</br>").replaceAll("\\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
+//            return extractedText.toString().replaceAll("\\r?\\n", "</br>").replaceAll("\\t", "&nbsp;&nbsp;&nbsp;&nbsp;");
+            return extractedText.toString().replaceAll("\\r?\\n", "</br>").replaceAll("&nbsp;", "");
         }catch (Exception e){
             e.printStackTrace();
         }finally {