This commit is contained in:
hejinbo
2023-11-25 17:56:52 +08:00
parent e3cd2495cf
commit 17b35f7095
3 changed files with 169 additions and 26 deletions
@@ -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<String> 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<String> findAndConvertPDF(File directory) {
List<String> 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<String> 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;
}
}