From 37c37868a3df6a5fcd58502ace637f17b838b7c8 Mon Sep 17 00:00:00 2001 From: chenkailing <632104866@qq.com> Date: Sat, 26 Dec 2020 02:31:34 +0800 Subject: [PATCH] =?UTF-8?q?Office=E7=BB=84=E4=BB=B6=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=BC=82=E6=AD=A5=E5=8C=96=EF=BC=8C=E6=8F=90=E9=80=9F=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E5=90=AF=E5=8A=A8=E9=80=9F=E5=BA=A6=E5=88=B05?= =?UTF-8?q?=E7=A7=92=E5=86=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../OfficeProcessManager.java} | 19 ++++++++++++++----- .../OfficeToPdfService.java} | 12 ++++++------ .../service/impl/OfficeFilePreviewImpl.java | 10 +++++----- 3 files changed, 25 insertions(+), 16 deletions(-) rename jodconverter-web/src/main/java/cn/keking/{utils/ConverterUtils.java => service/OfficeProcessManager.java} (90%) rename jodconverter-web/src/main/java/cn/keking/{utils/OfficeToPdf.java => service/OfficeToPdfService.java} (84%) diff --git a/jodconverter-web/src/main/java/cn/keking/utils/ConverterUtils.java b/jodconverter-web/src/main/java/cn/keking/service/OfficeProcessManager.java similarity index 90% rename from jodconverter-web/src/main/java/cn/keking/utils/ConverterUtils.java rename to jodconverter-web/src/main/java/cn/keking/service/OfficeProcessManager.java index 3f467787..acc4bf77 100644 --- a/jodconverter-web/src/main/java/cn/keking/utils/ConverterUtils.java +++ b/jodconverter-web/src/main/java/cn/keking/service/OfficeProcessManager.java @@ -1,4 +1,4 @@ -package cn.keking.utils; +package cn.keking.service; import com.sun.star.document.UpdateDocMode; import cn.keking.extend.ControlDocumentFormatRegistry; @@ -9,6 +9,8 @@ import org.artofsolving.jodconverter.office.OfficeManager; import org.artofsolving.jodconverter.office.OfficeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import javax.annotation.PostConstruct; @@ -29,16 +31,23 @@ import java.util.Properties; * @date 2017/11/13 */ @Component -public class ConverterUtils { +@Order(Ordered.HIGHEST_PRECEDENCE) +public class OfficeProcessManager { - private final Logger logger = LoggerFactory.getLogger(ConverterUtils.class); + private final Logger logger = LoggerFactory.getLogger(OfficeProcessManager.class); private OfficeManager officeManager; @PostConstruct public void initOfficeManager() { - File officeHome; - officeHome = OfficeUtils.getDefaultOfficeHome(); + new Thread(this::startOfficeManager).start(); + } + + /** + * 启动Office组件进程 + */ + private void startOfficeManager(){ + File officeHome = OfficeUtils.getDefaultOfficeHome(); if (officeHome == null) { throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误"); } diff --git a/jodconverter-web/src/main/java/cn/keking/utils/OfficeToPdf.java b/jodconverter-web/src/main/java/cn/keking/service/OfficeToPdfService.java similarity index 84% rename from jodconverter-web/src/main/java/cn/keking/utils/OfficeToPdf.java rename to jodconverter-web/src/main/java/cn/keking/service/OfficeToPdfService.java index ebffa46a..ba69da75 100644 --- a/jodconverter-web/src/main/java/cn/keking/utils/OfficeToPdf.java +++ b/jodconverter-web/src/main/java/cn/keking/service/OfficeToPdfService.java @@ -1,4 +1,4 @@ -package cn.keking.utils; +package cn.keking.service; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.springframework.stereotype.Component; @@ -9,11 +9,11 @@ import java.io.File; * @author yudian-it */ @Component -public class OfficeToPdf { - private final ConverterUtils converterUtils; +public class OfficeToPdfService { + private final OfficeProcessManager officeProcessManager; - public OfficeToPdf(ConverterUtils converterUtils) { - this.converterUtils = converterUtils; + public OfficeToPdfService(OfficeProcessManager officeProcessManager) { + this.officeProcessManager = officeProcessManager; } public void openOfficeToPDF(String inputFilePath, String outputFilePath) { @@ -33,7 +33,7 @@ public class OfficeToPdf { public void office2pdf(String inputFilePath, String outputFilePath) { - OfficeDocumentConverter converter = converterUtils.getDocumentConverter(); + OfficeDocumentConverter converter = officeProcessManager.getDocumentConverter(); if (null != inputFilePath) { File inputFile = new File(inputFilePath); // 判断目标文件路径是否为空 diff --git a/jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java b/jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java index d14ffaa5..aa85c835 100644 --- a/jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java +++ b/jodconverter-web/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java @@ -6,7 +6,7 @@ import cn.keking.model.ReturnResponse; import cn.keking.service.FilePreview; import cn.keking.utils.DownloadUtils; import cn.keking.utils.FileUtils; -import cn.keking.utils.OfficeToPdf; +import cn.keking.service.OfficeToPdfService; import cn.keking.utils.PdfUtils; import cn.keking.web.filter.BaseUrlFilter; import org.springframework.stereotype.Service; @@ -25,13 +25,13 @@ public class OfficeFilePreviewImpl implements FilePreview { private final FileUtils fileUtils; private final PdfUtils pdfUtils; private final DownloadUtils downloadUtils; - private final OfficeToPdf officeToPdf; + private final OfficeToPdfService officeToPdfService; - public OfficeFilePreviewImpl(FileUtils fileUtils, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdf officeToPdf) { + public OfficeFilePreviewImpl(FileUtils fileUtils, PdfUtils pdfUtils, DownloadUtils downloadUtils, OfficeToPdfService officeToPdfService) { this.fileUtils = fileUtils; this.pdfUtils = pdfUtils; this.downloadUtils = downloadUtils; - this.officeToPdf = officeToPdf; + this.officeToPdfService = officeToPdfService; } public static final String OFFICE_PREVIEW_TYPE_IMAGE = "image"; @@ -59,7 +59,7 @@ public class OfficeFilePreviewImpl implements FilePreview { } filePath = response.getContent(); if (StringUtils.hasText(outFilePath)) { - officeToPdf.openOfficeToPDF(filePath, outFilePath); + officeToPdfService.openOfficeToPDF(filePath, outFilePath); if (isHtml) { // 对转换后的文件进行操作(改变编码方式) fileUtils.doActionConvertedFile(outFilePath);