[测评系统]--测评系统核心代码库
zhijie
2022-09-19 e3f8089c830d5c6e7477090cf447bed65e80be9f
src/main/java/com/ots/project/tool/ShellTool.java
@@ -1,17 +1,42 @@
package com.ots.project.tool;
import com.ots.common.utils.spring.SpringUtils;
import com.ots.common.utils.text.CharsetKit;
import lombok.extern.slf4j.Slf4j;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.*;
import java.util.concurrent.ScheduledExecutorService;
@Slf4j
@Component
public class ShellTool {
    @Autowired
    private DocumentConverter documentConverter;
    private static ShellTool shellTool;
    @PostConstruct
    public void init(){
        shellTool = this;
        shellTool.documentConverter = documentConverter;
    }
    public static ShellTool builder(){
        return shellTool;
    }
    public static String execLibreofficeCommand(String type, String sourcePath, String targetPath) {
        String os = System.getProperty("os.name");
        StringBuilder builder = new StringBuilder();
        if (os.toLowerCase().startsWith("mac")) {
            builder.append("/Applications/LibreOffice.app/Contents/MacOS/soffice --headless --convert-to ");
        }  else if(os.toLowerCase().startsWith("windows")){
            builder.append("soffice.exe --headless --invisible --convert-to ");
        } else if(os.contains("Windows")){
            builder.append("cmd /c start soffice --headless --convert-to ");
        } else {
            builder.append("libreoffice6.4 --headless --convert-to ");
        }
@@ -25,9 +50,17 @@
                file.delete();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("执行命令:{}",builder.toString());
        return exec(builder.toString());
    }
    public static void execNewLibreofficeCommand(String type, String sourcePath, String targetPath) throws OfficeException {
        //使用文件方式转换成PDF
        shellTool.documentConverter.convert(new File(sourcePath)).to(new File(targetPath)).execute();
    }
    private static String exec(String command) {
        String returnString = "";
        Runtime runTime = Runtime.getRuntime();
@@ -51,6 +84,6 @@
        return returnString;
    }
    public static void main(String[] args) {
        System.out.println(exec("soffice.exe --headless --invisible --convert-to  D:/ots/uploadPath/upload/2021/02/28/01e01cf7e372ba8b1c5d24b8d69a46f8.docx 01e01cf7e372ba8b1c5d24b8d69a46f8.pdf "));
        //System.out.println(exec("soffice.exe --headless --invisible --convert-to  D:/ots/uploadPath/upload/2021/02/28/01e01cf7e372ba8b1c5d24b8d69a46f8.docx 01e01cf7e372ba8b1c5d24b8d69a46f8.pdf "));
    }
}