hejinbo 2 år sedan
förälder
incheckning
17b35f7095

+ 14
- 0
ruoyi-common/pom.xml Visa fil

@@ -164,6 +164,20 @@
164 164
             <version>1.9.1</version>
165 165
         </dependency>
166 166
 
167
+        <dependency>
168
+            <groupId>com.documents4j</groupId>
169
+            <artifactId>documents4j-local</artifactId>
170
+            <version>1.0.3</version>
171
+        </dependency>
172
+        <dependency>
173
+            <groupId>com.documents4j</groupId>
174
+            <artifactId>documents4j-transformer-msoffice-word</artifactId>
175
+            <version>1.0.3</version>
176
+        </dependency>
177
+
178
+
179
+
180
+
167 181
         <!--  发送邮件-->
168 182
         <dependency>
169 183
             <groupId>org.springframework.boot</groupId>

+ 102
- 25
ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java Visa fil

@@ -8,6 +8,9 @@ import cn.hutool.core.util.ZipUtil;
8 8
 import com.alibaba.fastjson.JSON;
9 9
 import com.alibaba.fastjson.JSONArray;
10 10
 import com.alibaba.fastjson.JSONObject;
11
+import com.documents4j.api.DocumentType;
12
+import com.documents4j.api.IConverter;
13
+import com.documents4j.job.LocalConverter;
11 14
 import com.google.gson.Gson;
12 15
 import com.google.gson.JsonArray;
13 16
 import com.google.gson.JsonObject;
@@ -47,6 +50,8 @@ import org.apache.http.entity.StringEntity;
47 50
 import org.apache.http.impl.client.CloseableHttpClient;
48 51
 import org.apache.http.impl.client.HttpClients;
49 52
 import org.apache.http.util.EntityUtils;
53
+import org.apache.poi.xwpf.usermodel.Document;
54
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
50 55
 import org.springframework.beans.factory.annotation.Autowired;
51 56
 import org.springframework.beans.factory.annotation.Value;
52 57
 import org.springframework.stereotype.Service;
@@ -2791,12 +2796,15 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
2791 2796
             return AjaxResult.error("请选择要上传的文件");
2792 2797
         }
2793 2798
         //String targetPath = "/home/ruoyi/uploadPath/upload/unzipFile";
2794
-        String targetPath = "D:\\home\\unzip\\";
2799
+        UUID uuid = UUID.randomUUID();
2800
+        String targetPath = "D:\\home\\unzip\\"+uuid+"\\";
2795 2801
         File zipFile = null;
2796 2802
         InputStream ins = null;
2797 2803
         try {
2798 2804
             ins = file.getInputStream();
2799
-            zipFile = new File(file.getOriginalFilename());
2805
+            String savePath = "D:\\develop\\java\\";
2806
+            String saveName = uuid+file.getOriginalFilename();
2807
+            zipFile = new File(savePath+saveName);
2800 2808
             inputChangeToFile(ins, zipFile);
2801 2809
         } catch (IOException e) {
2802 2810
             e.printStackTrace();
@@ -2804,12 +2812,14 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
2804 2812
         //解压缩上传的压缩包
2805 2813
         boolean unzipSuccess  = UnZipFileUtils.unZipFile(zipFile, targetPath);
2806 2814
         if (unzipSuccess ) {
2807
-            String unzipPath = targetPath + file.getOriginalFilename() + "\\";
2815
+            //去掉后缀名,拿到解压后的文件夹路径
2816
+            String fileName = file.getOriginalFilename();
2817
+            String nameWithoutExtension = fileName.substring(0, fileName.lastIndexOf("."));
2808 2818
             File directory = new File(targetPath);
2809
-            String pdfFilePath  = findAndConvertPDF(directory);
2810
-            if (pdfFilePath != null){
2819
+            List<String> andConvertPDF = findAndConvertPDF(directory);
2820
+            if (andConvertPDF != null && andConvertPDF.size() > 0){
2811 2821
                 // 返回找到的PDF文件路径
2812
-                return AjaxResult.success(pdfFilePath);
2822
+                return AjaxResult.success(andConvertPDF);
2813 2823
             }else {
2814 2824
                 // 没有找到符合条件的文件
2815 2825
                 return AjaxResult.error("未找到符合条件的文件");
@@ -2820,33 +2830,100 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService {
2820 2830
         }
2821 2831
 
2822 2832
     }
2823
-    public static String findAndConvertPDF(File directory) {
2824
-
2825
-        File[] matches = directory.listFiles(new FilenameFilter() {
2826
-            public boolean accept(File dir, String name) {
2827
-                return name.contains("仲裁申请书");
2828
-            }
2829
-        });
2830
-
2831
-        if (matches != null && matches.length > 0) {
2832
-            for (File file : matches) {
2833
-                if (file.isFile() && file.getName().toLowerCase().endsWith(".pdf")) {
2834
-                    // 如果是PDF格式直接返回路径
2835
-                    return file.getAbsolutePath();
2833
+    public static List<String> findAndConvertPDF(File directory) {
2834
+        List<String> pdfPaths = new ArrayList<>();
2835
+        if (directory.isFile()) {
2836
+            String path = "";
2837
+            // 如果传入的参数是一个文件
2838
+            if (directory.getName().contains("仲裁申请书")) {
2839
+                if (isPDF(directory)) {
2840
+                    // 如果文件名包含"仲裁申请书"且是PDF格式,直接返回路径
2841
+                    path = directory.getAbsolutePath();
2836 2842
                 } else {
2837 2843
                     // 如果不是PDF格式,进行转换成PDF并返回路径
2838
-                    String pdfPath = convertToPDF(file);
2839
-                    return pdfPath;
2844
+                    String pdfPath = convertToPDF(directory);
2845
+                    if (pdfPath != null) {
2846
+                        path = pdfPath;
2847
+                    }
2848
+                }
2849
+                if (path!= null) {
2850
+                    pdfPaths.add(path);
2840 2851
                 }
2841 2852
             }
2853
+        } else if (directory.isDirectory()) {
2854
+            searchAndConvertPDF(directory, pdfPaths);
2855
+        }else {
2856
+            return null;
2842 2857
         }
2843
-        return null;
2858
+      return pdfPaths;
2859
+    }
2860
+
2861
+    public static boolean isPDF(File file) {
2862
+        String extension = getFileExtension(file);
2863
+        return extension.equalsIgnoreCase("pdf");
2844 2864
     }
2845 2865
 
2866
+    public static String getFileExtension(File file) {
2867
+        String name = file.getName();
2868
+        int lastIndexOfDot = name.lastIndexOf(".");
2869
+        if (lastIndexOfDot != -1 && lastIndexOfDot < name.length() - 1) {
2870
+            return name.substring(lastIndexOfDot + 1);
2871
+        } else {
2872
+            return "";
2873
+        }
2874
+    }
2875
+    public static void searchAndConvertPDF(File directory, List<String> pdfPaths) {
2876
+        File[] files = directory.listFiles();
2877
+        if (files != null) {
2878
+            for (File file : files) {
2879
+                if (file.isFile()) {
2880
+                    // 如果是文件且文件名包含"仲裁申请书"
2881
+                    if (file.getName().contains("仲裁申请书")) {
2882
+                        if (isPDF(file)) {
2883
+                            // 如果是PDF格式,直接添加到列表中
2884
+                            pdfPaths.add(file.getAbsolutePath());
2885
+                        } else {
2886
+                            // 如果不是PDF格式,进行转换成PDF并添加转换后的路径到列表中
2887
+                            String pdfPath = convertToPDF(file);
2888
+                            if (pdfPath != null) {
2889
+                                pdfPaths.add(pdfPath);
2890
+                            }
2891
+                        }
2892
+                    }
2893
+                } else if (file.isDirectory()) {
2894
+                    // 如果是目录,递归查找
2895
+                    searchAndConvertPDF(file, pdfPaths);
2896
+                }
2897
+            }
2898
+        }
2899
+    }
2846 2900
     private static String convertToPDF(File file) {
2847
-        // 实现文件格式转换逻辑,这里假设已经实现了转换逻辑,返回转换后的PDF文件路径
2848
-        String pdfPath = "convertedFilePath.pdf"; // 这里替换为实际转换后的PDF文件路径
2849
-        return pdfPath;
2901
+        String wordFilePath = file.getAbsolutePath();
2902
+        String pdfSaveDirectory ="D:\\home\\unzip\\wordToPDF\\";
2903
+        File directory = new File(pdfSaveDirectory);
2904
+        if (!directory.exists()) {
2905
+            directory.mkdirs();
2906
+        }
2907
+        String name = file.getName();
2908
+        String nameWithoutExtension = name.substring(0, name.lastIndexOf("."));
2909
+        String pdfFilePath =pdfSaveDirectory+nameWithoutExtension+".pdf";
2910
+        File inputWord = new File(wordFilePath);
2911
+        File outputFile = new File(pdfFilePath);
2912
+        try  {
2913
+            InputStream docxInputStream = new FileInputStream(inputWord);
2914
+            OutputStream outputStream = new FileOutputStream(outputFile);
2915
+            IConverter converter = LocalConverter.builder().build();
2916
+            converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute();
2917
+            docxInputStream.close();
2918
+            outputStream.close();
2919
+            System.out.println("success");
2920
+            File file1= new File(pdfFilePath);
2921
+            System.out.println("这是转化后的PDF文件路径"+file1);
2922
+
2923
+        } catch (Exception e) {
2924
+            e.printStackTrace();
2925
+        }
2926
+        return pdfFilePath;
2850 2927
     }
2851 2928
 }
2852 2929
 

+ 52
- 0
ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java Visa fil

@@ -0,0 +1,52 @@
1
+package com.ruoyi.wisdomarbitrate.utils;
2
+
3
+import com.documents4j.api.DocumentType;
4
+import com.documents4j.api.IConverter;
5
+import com.documents4j.job.LocalConverter;
6
+import com.tencentcloudapi.common.Credential;
7
+import com.tencentcloudapi.common.exception.TencentCloudSDKException;
8
+import com.tencentcloudapi.common.profile.ClientProfile;
9
+import com.tencentcloudapi.common.profile.HttpProfile;
10
+import com.tencentcloudapi.ocr.v20181119.OcrClient;
11
+import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRRequest;
12
+import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRResponse;
13
+
14
+
15
+import java.io.*;
16
+
17
+public class Tset {
18
+    public static void main(String[] args) {
19
+        //API的SecretId
20
+       final String SECRET_ID = "AKIDeEf2A8uX1HSainvvnXAc3X9ZlhtyvkMp";
21
+        //API的SecretKey
22
+         final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V";
23
+
24
+        String pdfFilePath = "D:\\data\\1. 仲裁申请书-陈博.pdf";
25
+
26
+        try{
27
+            // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
28
+            // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
29
+            // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
30
+            Credential cred = new Credential(SECRET_ID, SECRET_KEY);
31
+            // 实例化一个http选项,可选的,没有特殊需求可以跳过
32
+            HttpProfile httpProfile = new HttpProfile();
33
+            httpProfile.setEndpoint("ocr.tencentcloudapi.com");
34
+            // 实例化一个client选项,可选的,没有特殊需求可以跳过
35
+            ClientProfile clientProfile = new ClientProfile();
36
+            clientProfile.setHttpProfile(httpProfile);
37
+            // 实例化要请求产品的client对象,clientProfile是可选的
38
+            OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile);
39
+            // 实例化一个请求对象,每个接口都会对应一个request对象
40
+            GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest();
41
+            req.setImageUrl(pdfFilePath);
42
+            req.setIsPdf(true);
43
+            req.setPdfPageNumber(1L);
44
+            // 返回的resp是一个GeneralAccurateOCRResponse的实例,与请求对象对应
45
+            GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req);
46
+            // 输出json格式的字符串回包
47
+            System.out.println(GeneralAccurateOCRResponse.toJsonString(resp));
48
+        } catch (TencentCloudSDKException e) {
49
+            System.out.println(e.toString());
50
+        }
51
+    }
52
+}