重写代码

This commit is contained in:
chenxudong 2025-01-21 16:04:49 +08:00
parent 7450f41a9d
commit 53e61b11d4
8 changed files with 12 additions and 13 deletions

View File

@ -10,7 +10,7 @@ import java.io.InputStream;
public class ExcelParse extends FileParse { public class ExcelParse extends FileParse {
@Override @Override
public String parseContent(InputStream stream, String fileType) { public String parseAllText(InputStream stream, String fileType) {
String fileTmpPath = createFileTmpPath(fileType); String fileTmpPath = createFileTmpPath(fileType);
String res = ""; String res = "";
try { try {

View File

@ -9,7 +9,7 @@ public abstract class FileParse {
private static String tmpPath = Thread.currentThread().getContextClassLoader().getResource("").getPath(); private static String tmpPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
public abstract String parseContent(InputStream stream, String fileType); public abstract String parseAllText(InputStream stream, String fileType);
protected String createFileTmpPath(String fileType) { protected String createFileTmpPath(String fileType) {
String uuid = IdUtil.fastSimpleUUID(); String uuid = IdUtil.fastSimpleUUID();

View File

@ -9,7 +9,7 @@ import java.io.InputStream;
@Slf4j @Slf4j
public class PdfParse extends FileParse { public class PdfParse extends FileParse {
@Override @Override
public String parseContent(InputStream stream, String fileType) { public String parseAllText(InputStream stream, String fileType) {
String res = ""; String res = "";
String fileTmpPath = createFileTmpPath(fileType); String fileTmpPath = createFileTmpPath(fileType);
try { try {

View File

@ -10,15 +10,15 @@ import java.io.InputStream;
public class PptParse extends FileParse { public class PptParse extends FileParse {
@Override @Override
public String parseContent(InputStream stream, String fileType) { public String parseAllText(InputStream stream, String fileType) {
String fileTmpPath = createFileTmpPath(fileType); String fileTmpPath = createFileTmpPath(fileType);
String res = ""; String res = "";
try { try {
FileUtil.writeFromStream(stream, fileTmpPath); FileUtil.writeFromStream(stream, fileTmpPath);
if (fileType.endsWith("pptx")) { if (fileType.endsWith("pptx")) {
return OfficeFileUtil.parsePptxAllText(fileTmpPath); res = OfficeFileUtil.parsePptxAllText(fileTmpPath);
} }
return OfficeFileUtil.parsePptAllText(fileTmpPath); res = OfficeFileUtil.parsePptAllText(fileTmpPath);
} catch (Exception e) { } catch (Exception e) {
log.error("解析{}格式的ppt错误具体为{}",fileType, e.getMessage(), e); log.error("解析{}格式的ppt错误具体为{}",fileType, e.getMessage(), e);
} }

View File

@ -10,7 +10,7 @@ import java.nio.charset.Charset;
public class TextParse extends FileParse { public class TextParse extends FileParse {
@Override @Override
public String parseContent(InputStream stream, String fileType) { public String parseAllText(InputStream stream, String fileType) {
String fileTmpPath = createFileTmpPath(fileType); String fileTmpPath = createFileTmpPath(fileType);
String res = ""; String res = "";
try { try {

View File

@ -10,7 +10,7 @@ import java.io.InputStream;
public class WordParse extends FileParse { public class WordParse extends FileParse {
@Override @Override
public String parseContent(InputStream stream, String fileType) { public String parseAllText(InputStream stream, String fileType) {
String fileTmpPath = createFileTmpPath(fileType); String fileTmpPath = createFileTmpPath(fileType);
String res = ""; String res = "";
try { try {

View File

@ -81,7 +81,7 @@ public final class EleCommonUtil {
public static String parse(InputStream inputStream, String fileType) { public static String parse(InputStream inputStream, String fileType) {
FileParse fileParse = PARSE_MAP.getOrDefault(fileType, new TextParse()); FileParse fileParse = PARSE_MAP.getOrDefault(fileType, new TextParse());
return fileParse.parseContent(inputStream, fileType); return fileParse.parseAllText(inputStream, fileType);
} }
} }

View File

@ -33,21 +33,20 @@ import java.util.Objects;
@Slf4j @Slf4j
public class OfficeFileUtil { public class OfficeFileUtil {
private static final IConverter CONVERT = LocalConverter.builder().build();
public static void doc2pdf(String wordPath, String pdfPath) { public static void doc2pdf(String wordPath, String pdfPath) {
if (EleCommonUtil.isWinOs()) { if (EleCommonUtil.isWinOs()) {
File inputWord = new File(wordPath); File inputWord = new File(wordPath);
File outputFile = new File(pdfPath); File outputFile = new File(pdfPath);
try(InputStream docxInputStream = Files.newInputStream(inputWord.toPath()); try(InputStream docxInputStream = Files.newInputStream(inputWord.toPath());
OutputStream outputStream = Files.newOutputStream(outputFile.toPath())) { OutputStream outputStream = Files.newOutputStream(outputFile.toPath())) {
boolean execute = CONVERT.convert(docxInputStream) IConverter build = LocalConverter.builder().build();
boolean execute = build.convert(docxInputStream)
.as(DocumentType.DOCX) .as(DocumentType.DOCX)
.to(outputStream) .to(outputStream)
.as(DocumentType.PDF).schedule().get(); .as(DocumentType.PDF).schedule().get();
Assert.isTrue(execute, "转换失败"); Assert.isTrue(execute, "转换失败");
log.info("转换完毕 targetPath = {}", outputFile.getAbsolutePath()); log.info("转换完毕 targetPath = {}", outputFile.getAbsolutePath());
CONVERT.shutDown(); build.shutDown();
} catch (Exception e) { } catch (Exception e) {
log.error("[documents4J] word转pdf失败:{}", e.toString()); log.error("[documents4J] word转pdf失败:{}", e.toString());
String info = StrFormatter.format("word文档{}转换成pdf文档{}失败", wordPath, pdfPath); String info = StrFormatter.format("word文档{}转换成pdf文档{}失败", wordPath, pdfPath);