From 17b35f7095f4b7ed7a298ecfbf57e12673d7ce8f Mon Sep 17 00:00:00 2001 From: hejinbo Date: Sat, 25 Nov 2023 17:56:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9A=82=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ruoyi-common/pom.xml | 14 ++ .../impl/CaseApplicationServiceImpl.java | 129 ++++++++++++++---- .../com/ruoyi/wisdomarbitrate/utils/Tset.java | 52 +++++++ 3 files changed, 169 insertions(+), 26 deletions(-) create mode 100644 ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java diff --git a/ruoyi-common/pom.xml b/ruoyi-common/pom.xml index b0c399b..2a11d03 100644 --- a/ruoyi-common/pom.xml +++ b/ruoyi-common/pom.xml @@ -164,6 +164,20 @@ 1.9.1 + + com.documents4j + documents4j-local + 1.0.3 + + + com.documents4j + documents4j-transformer-msoffice-word + 1.0.3 + + + + + org.springframework.boot diff --git a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java index 7c7616d..c44bb55 100644 --- a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java +++ b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/service/impl/CaseApplicationServiceImpl.java @@ -8,6 +8,9 @@ import cn.hutool.core.util.ZipUtil; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.documents4j.api.DocumentType; +import com.documents4j.api.IConverter; +import com.documents4j.job.LocalConverter; import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; @@ -47,6 +50,8 @@ import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; +import org.apache.poi.xwpf.usermodel.Document; +import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; @@ -2791,12 +2796,15 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { return AjaxResult.error("请选择要上传的文件"); } //String targetPath = "/home/ruoyi/uploadPath/upload/unzipFile"; - String targetPath = "D:\\home\\unzip\\"; + UUID uuid = UUID.randomUUID(); + String targetPath = "D:\\home\\unzip\\"+uuid+"\\"; File zipFile = null; InputStream ins = null; try { ins = file.getInputStream(); - zipFile = new File(file.getOriginalFilename()); + String savePath = "D:\\develop\\java\\"; + String saveName = uuid+file.getOriginalFilename(); + zipFile = new File(savePath+saveName); inputChangeToFile(ins, zipFile); } catch (IOException e) { e.printStackTrace(); @@ -2804,12 +2812,14 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { //解压缩上传的压缩包 boolean unzipSuccess = UnZipFileUtils.unZipFile(zipFile, targetPath); if (unzipSuccess ) { - String unzipPath = targetPath + file.getOriginalFilename() + "\\"; + //去掉后缀名,拿到解压后的文件夹路径 + String fileName = file.getOriginalFilename(); + String nameWithoutExtension = fileName.substring(0, fileName.lastIndexOf(".")); File directory = new File(targetPath); - String pdfFilePath = findAndConvertPDF(directory); - if (pdfFilePath != null){ + List andConvertPDF = findAndConvertPDF(directory); + if (andConvertPDF != null && andConvertPDF.size() > 0){ // 返回找到的PDF文件路径 - return AjaxResult.success(pdfFilePath); + return AjaxResult.success(andConvertPDF); }else { // 没有找到符合条件的文件 return AjaxResult.error("未找到符合条件的文件"); @@ -2820,33 +2830,100 @@ public class CaseApplicationServiceImpl implements ICaseApplicationService { } } - public static String findAndConvertPDF(File directory) { - - File[] matches = directory.listFiles(new FilenameFilter() { - public boolean accept(File dir, String name) { - return name.contains("仲裁申请书"); - } - }); - - if (matches != null && matches.length > 0) { - for (File file : matches) { - if (file.isFile() && file.getName().toLowerCase().endsWith(".pdf")) { - // 如果是PDF格式直接返回路径 - return file.getAbsolutePath(); + public static List findAndConvertPDF(File directory) { + List pdfPaths = new ArrayList<>(); + if (directory.isFile()) { + String path = ""; + // 如果传入的参数是一个文件 + if (directory.getName().contains("仲裁申请书")) { + if (isPDF(directory)) { + // 如果文件名包含"仲裁申请书"且是PDF格式,直接返回路径 + path = directory.getAbsolutePath(); } else { // 如果不是PDF格式,进行转换成PDF并返回路径 - String pdfPath = convertToPDF(file); - return pdfPath; + String pdfPath = convertToPDF(directory); + if (pdfPath != null) { + path = pdfPath; + } + } + if (path!= null) { + pdfPaths.add(path); + } + } + } else if (directory.isDirectory()) { + searchAndConvertPDF(directory, pdfPaths); + }else { + return null; + } + return pdfPaths; + } + + public static boolean isPDF(File file) { + String extension = getFileExtension(file); + return extension.equalsIgnoreCase("pdf"); + } + + public static String getFileExtension(File file) { + String name = file.getName(); + int lastIndexOfDot = name.lastIndexOf("."); + if (lastIndexOfDot != -1 && lastIndexOfDot < name.length() - 1) { + return name.substring(lastIndexOfDot + 1); + } else { + return ""; + } + } + public static void searchAndConvertPDF(File directory, List pdfPaths) { + File[] files = directory.listFiles(); + if (files != null) { + for (File file : files) { + if (file.isFile()) { + // 如果是文件且文件名包含"仲裁申请书" + if (file.getName().contains("仲裁申请书")) { + if (isPDF(file)) { + // 如果是PDF格式,直接添加到列表中 + pdfPaths.add(file.getAbsolutePath()); + } else { + // 如果不是PDF格式,进行转换成PDF并添加转换后的路径到列表中 + String pdfPath = convertToPDF(file); + if (pdfPath != null) { + pdfPaths.add(pdfPath); + } + } + } + } else if (file.isDirectory()) { + // 如果是目录,递归查找 + searchAndConvertPDF(file, pdfPaths); } } } - return null; } - private static String convertToPDF(File file) { - // 实现文件格式转换逻辑,这里假设已经实现了转换逻辑,返回转换后的PDF文件路径 - String pdfPath = "convertedFilePath.pdf"; // 这里替换为实际转换后的PDF文件路径 - return pdfPath; + String wordFilePath = file.getAbsolutePath(); + String pdfSaveDirectory ="D:\\home\\unzip\\wordToPDF\\"; + File directory = new File(pdfSaveDirectory); + if (!directory.exists()) { + directory.mkdirs(); + } + String name = file.getName(); + String nameWithoutExtension = name.substring(0, name.lastIndexOf(".")); + String pdfFilePath =pdfSaveDirectory+nameWithoutExtension+".pdf"; + File inputWord = new File(wordFilePath); + File outputFile = new File(pdfFilePath); + try { + InputStream docxInputStream = new FileInputStream(inputWord); + OutputStream outputStream = new FileOutputStream(outputFile); + IConverter converter = LocalConverter.builder().build(); + converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); + docxInputStream.close(); + outputStream.close(); + System.out.println("success"); + File file1= new File(pdfFilePath); + System.out.println("这是转化后的PDF文件路径"+file1); + + } catch (Exception e) { + e.printStackTrace(); + } + return pdfFilePath; } } diff --git a/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java new file mode 100644 index 0000000..7f3a6b6 --- /dev/null +++ b/ruoyi-system/src/main/java/com/ruoyi/wisdomarbitrate/utils/Tset.java @@ -0,0 +1,52 @@ +package com.ruoyi.wisdomarbitrate.utils; + +import com.documents4j.api.DocumentType; +import com.documents4j.api.IConverter; +import com.documents4j.job.LocalConverter; +import com.tencentcloudapi.common.Credential; +import com.tencentcloudapi.common.exception.TencentCloudSDKException; +import com.tencentcloudapi.common.profile.ClientProfile; +import com.tencentcloudapi.common.profile.HttpProfile; +import com.tencentcloudapi.ocr.v20181119.OcrClient; +import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRRequest; +import com.tencentcloudapi.ocr.v20181119.models.GeneralAccurateOCRResponse; + + +import java.io.*; + +public class Tset { + public static void main(String[] args) { + //API的SecretId + final String SECRET_ID = "AKIDeEf2A8uX1HSainvvnXAc3X9ZlhtyvkMp"; + //API的SecretKey + final String SECRET_KEY = "QjphKo8zkHZigT8j9PVtFPJyfIvO3d6V"; + + String pdfFilePath = "D:\\data\\1. 仲裁申请书-陈博.pdf"; + + try{ + // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 + // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 + // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 + Credential cred = new Credential(SECRET_ID, SECRET_KEY); + // 实例化一个http选项,可选的,没有特殊需求可以跳过 + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("ocr.tencentcloudapi.com"); + // 实例化一个client选项,可选的,没有特殊需求可以跳过 + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 实例化要请求产品的client对象,clientProfile是可选的 + OcrClient client = new OcrClient(cred, "ap-beijing", clientProfile); + // 实例化一个请求对象,每个接口都会对应一个request对象 + GeneralAccurateOCRRequest req = new GeneralAccurateOCRRequest(); + req.setImageUrl(pdfFilePath); + req.setIsPdf(true); + req.setPdfPageNumber(1L); + // 返回的resp是一个GeneralAccurateOCRResponse的实例,与请求对象对应 + GeneralAccurateOCRResponse resp = client.GeneralAccurateOCR(req); + // 输出json格式的字符串回包 + System.out.println(GeneralAccurateOCRResponse.toJsonString(resp)); + } catch (TencentCloudSDKException e) { + System.out.println(e.toString()); + } + } +} \ No newline at end of file