[测评系统]--测评系统核心代码库
linzhijie
2022-10-21 2f4cc85cd586618865ad2892fa378fff123613f9
更改转换方法
1个文件已添加
1个文件已修改
206 ■■■■■ 已修改文件
src/main/java/com/ots/project/tool/ShellTool.java 13 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ots/project/tool/libreoffice/Office2PDFUtils.java 193 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/ots/project/tool/ShellTool.java
@@ -1,6 +1,7 @@
package com.ots.project.tool;
import com.ots.common.utils.spring.SpringUtils;
import com.ots.common.utils.text.CharsetKit;
import com.ots.project.tool.libreoffice.Office2PDFUtils;
import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
@@ -15,14 +16,19 @@
@Component
public class ShellTool {
//    @Autowired
//    private DocumentConverter documentConverter;
    @Autowired
    private DocumentConverter documentConverter;
    private Office2PDFUtils office2PDFUtils;
    private static ShellTool shellTool;
    @PostConstruct
    public void init(){
        shellTool = this;
        shellTool.documentConverter = documentConverter;
        //shellTool.documentConverter = documentConverter;
        shellTool.office2PDFUtils = office2PDFUtils;
    }
    public static ShellTool builder(){
@@ -58,7 +64,8 @@
    public static void execNewLibreofficeCommand(String type, String sourcePath, String targetPath) throws OfficeException {
        //使用文件方式转换成PDF
        shellTool.documentConverter.convert(new File(sourcePath)).to(new File(targetPath)).execute();
        //shellTool.documentConverter.convert(new File(sourcePath)).to(new File(targetPath)).execute();
        shellTool.office2PDFUtils.office2Pdf(sourcePath,targetPath);
    }
    private static String exec(String command) {
src/main/java/com/ots/project/tool/libreoffice/Office2PDFUtils.java
New file
@@ -0,0 +1,193 @@
package com.ots.project.tool.libreoffice;
import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.jodconverter.core.office.OfficeManager;
import org.jodconverter.local.LocalConverter;
import org.jodconverter.local.office.LocalOfficeManager;
import org.jodconverter.local.process.WindowsProcessManager;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.File;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
 * @version 1.0
 * @Author linzhijie
 * @Description TODO
 * @Date 2022/10/21  10:03
 */
@Slf4j
@Component
public class Office2PDFUtils {
    @Value("${jodconverter.local.office-home}")
    private String officeHome;
    @Value("${jodconverter.local.portNumbers}")
    private String portNumbers;
    private static BlockingQueue<OfficeManager> OfficeManagerQueue = new LinkedBlockingDeque<>();
    private static Lock lock = new ReentrantLock();
    /**
     * @Description: 使Office2003-2007全部格式的文档(.doc|.docx|.xls|.xlsx|.ppt|.pptx) 转化为pdf文件
     * @method: openOfficeToPDF
     * @Param: inputFilePath
     * @Param: outputFilePath
     * @return: boolean
     * @auther: LHL
     */
    public boolean openOfficeToPDF(String inputFilePath, String outputFilePath) {
        return office2Pdf(inputFilePath, outputFilePath);
    }
    /**
     * @Description: 根据操作系统的名称,获取OpenOffice.org 的安装目录
     * @method: getOfficeHome
     * @Param:
     * @return: java.lang.String OpenOffice.org 的安装目录
     * @auther: LHL
     */
/*    private String getOfficeHome() {
        String osName = System.getProperty("os.name");
        log.debug("操作系统名称:" + osName);
        if (Pattern.matches("Linux.*", osName)) {
            return linuxPath;
        } else if (Pattern.matches("Windows.*", osName)) {
            return winPath;
        } else if (Pattern.matches("Mac.*", osName)) {
            return winPath;
        }
        return null;
    }*/
    /**
     * @Description: 在bean创建实例时, 连接OpenOffice.org 并且启动OpenOffice.org
     * @method: initOpenOfficeService
     * @Param:
     * @return: void
     * @auther: LHL
     */
    @PostConstruct
    public void initOpenOfficeService() {
        LocalOfficeManager.Builder builder = LocalOfficeManager.builder();
        builder.officeHome(officeHome);
        builder.portNumbers(2002);
        builder.processTimeout(12000L);
        builder.processRetryInterval(250L);
        builder.maxTasksPerProcess(200);
        builder.taskQueueTimeout(30000L);
        for (String port : portNumbers.split(",")) {
            builder.portNumbers(Integer.valueOf(port));
            OfficeManager OfficeManager = builder.build();
            try {
                OfficeManager.start();
                log.debug("***********************officeManager 启动!***********************");
            } catch (OfficeException e) {
                //打印日志
                log.debug("***********************启动 openOffice 失败!***********************");
                e.printStackTrace();
            }
            try {
                OfficeManagerQueue.put(OfficeManager);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * @Description: bean销毁时停止
     * @method: destroyOpenOfficeService
     * @Param:
     * @return: void
     * @auther: LHL
     */
    @PreDestroy
    public void destroyOpenOfficeService() {
        for (OfficeManager manager : OfficeManagerQueue) {
            try {
                log.debug("关闭 officeManager");
                manager.stop();
            } catch (OfficeException e) {
                log.debug("officeManager关闭失败!" + e.getMessage());
                e.printStackTrace();
            }
        }
    }
    /**
     * @Description: 转换文件
     * @method: convertFile
     * @Param: inputFile
     * @Param: outputFilePath
     * @Param: officeManager
     * @return: java.io.File
     * @auther: LHL
     */
    private File convertFile(File inputFile, String outputFilePath, OfficeManager officeManager) throws OfficeException {
        File outputFile = new File(outputFilePath);
        // 假如目标路径不存在,则新建该路径
        if (!outputFile.getParentFile().exists()) {
            outputFile.getParentFile().mkdirs();
        }
        DocumentConverter converter = LocalConverter.builder()
                .officeManager(officeManager)
                .build();
        // 锁
        lock.lock();
        //converter.convert(inputFile).to(outputFile).execute();
        converter.convert(inputFile).to(outputFile).execute();
        //释放锁
        lock.unlock();
        try {
            OfficeManagerQueue.put(officeManager);
            log.debug("blockingQueue puted OfficeManagerQueue size :" + OfficeManagerQueue.size());
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        log.debug("文件:" + inputFile.getAbsolutePath() + "\n转换为目标文件:" + outputFile + "\n成功!");
        return outputFile;
    }
    /**
     * 文件名需要唯一
     */
    public boolean office2Pdf(String inputFilePath, String outputFilePath) {
        OfficeManager officeManager = null;
        try {
            if (StringUtils.isEmpty(inputFilePath)) {
                log.debug("输入文件地址为空,转换终止!");
                return false;
            }
            File inputFile = new File(inputFilePath);
            if (!inputFile.exists()) {
                log.debug("输入文件不存在,转换终止!");
                return false;
            }
            officeManager = OfficeManagerQueue.take();
            log.debug("blockingQueue taked , OfficeManagerQueue size :" + OfficeManagerQueue.size());
            File file = convertFile(inputFile, outputFilePath, officeManager);
            if (!file.exists()) {
                log.debug("转换文件不存在!转换失败!!");
                return false;
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            log.debug("转换异常");
            return false;
        }
    }
}