| | |
| | | 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(); |
| | |
| | | } else { |
| | | builder.append("libreoffice6.4 --headless --convert-to "); |
| | | } |
| | | |
| | | builder.append(type).append(" "); |
| | | builder.append(type).append(":impress_pdf_Export "); |
| | | builder.append("--outdir " + targetPath).append(" "); |
| | | builder.append(sourcePath).append(" "); |
| | | |
| | |
| | | file.delete(); |
| | | } |
| | | } catch (Exception e) { |
| | | } |
| | | return run_cmd(builder.toString()); |
| | | } |
| | | |
| | | public static String run_cmd(String strcmd) { |
| | | // |
| | | Runtime rt = Runtime.getRuntime(); //Runtime.getRuntime()返回当前应用程序的Runtime对象 |
| | | Process ps = null; //Process可以控制该子进程的执行或获取该子进程的信息。 |
| | | try { |
| | | ps = rt.exec(strcmd); //该对象的exec()方法指示Java虚拟机创建一个子进程执行指定的可执行程序,并返回与该子进程对应的Process对象实例。 |
| | | ps.waitFor(); //等待子进程完成再往下执行。 |
| | | } catch (IOException e1) { |
| | | e1.printStackTrace(); |
| | | } catch (InterruptedException e) { |
| | | // TODO Auto-generated catch block |
| | | e.printStackTrace(); |
| | | } |
| | | log.info("执行命令:{}",builder.toString()); |
| | | return exec(builder.toString()); |
| | | } |
| | | |
| | | int i = ps.exitValue(); //接收执行完毕的返回值 |
| | | if (i == 0) { |
| | | System.out.println("执行完成."); |
| | | } else { |
| | | System.out.println("执行失败."); |
| | | } |
| | | |
| | | ps.destroy(); //销毁子进程 |
| | | ps = null; |
| | | |
| | | return null; |
| | | 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) { |
| | |
| | | return returnString; |
| | | } |
| | | public static void main(String[] args) { |
| | | System.out.println(exec("libreoffice6.4 --headless --convert-to /root/小红.docx 小红.pdf ")); |
| | | //System.out.println(exec("soffice.exe --headless --invisible --convert-to D:/ots/uploadPath/upload/2021/02/28/01e01cf7e372ba8b1c5d24b8d69a46f8.docx 01e01cf7e372ba8b1c5d24b8d69a46f8.pdf ")); |
| | | } |
| | | } |