From 3e43c2f9d0b41c4210677e96525c03760cc37eaa Mon Sep 17 00:00:00 2001 From: tomhusky Date: Thu, 21 Jul 2022 03:50:09 +0000 Subject: [PATCH] =?UTF-8?q?!49=20=E4=BF=AE=E5=A4=8DLinux=E7=8E=AF=E5=A2=83?= =?UTF-8?q?=E4=B8=ADOfficePluginManager=E4=B8=ADKillProcess=E5=87=BA?= =?UTF-8?q?=E7=8E=B0=E6=9D=80=E6=8E=89=E8=BF=9B=E7=A8=8B=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=EF=BC=8C=E5=AF=BC=E8=87=B4=E4=BA=8C=E6=AC=A1=E5=90=AF=E5=8A=A8?= =?UTF-8?q?=E5=A4=B1=E8=B4=A5=E7=9A=84bug=20*=20=E4=BF=AE=E5=A4=8DLinux?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=E4=B8=ADOfficePluginManager=E4=B8=ADKillProc?= =?UTF-8?q?ess=E5=87=BA=E7=8E=B0=E6=9D=80=E6=8E=89=E5=B7=B2=E7=BB=8F?= =?UTF-8?q?=E5=AD=98=E5=9C=A8=E8=BF=9B=E7=A8=8B=E5=A4=B1=E8=B4=A5=EF=BC=8C?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E4=BA=8C=E6=AC=A1=E5=90=AF=E5=8A=A8=E5=A4=B1?= =?UTF-8?q?=E8=B4=A5=E7=9A=84bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../keking/service/OfficePluginManager.java | 33 +++++++++++++------ 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/server/src/main/java/cn/keking/service/OfficePluginManager.java b/server/src/main/java/cn/keking/service/OfficePluginManager.java index 9b8e3183..f6cafef2 100644 --- a/server/src/main/java/cn/keking/service/OfficePluginManager.java +++ b/server/src/main/java/cn/keking/service/OfficePluginManager.java @@ -6,6 +6,7 @@ import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; import org.artofsolving.jodconverter.office.OfficeUtils; +import org.artofsolving.jodconverter.util.PlatformUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; @@ -24,7 +25,6 @@ import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.HashMap; import java.util.Map; -import java.util.Properties; /** * 创建文件转换器 @@ -50,7 +50,7 @@ public class OfficePluginManager { * 启动Office组件进程 */ @PostConstruct - public void startOfficeManager(){ + public void startOfficeManager() { File officeHome = OfficeUtils.getDefaultOfficeHome(); if (officeHome == null) { throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误"); @@ -62,7 +62,7 @@ public class OfficePluginManager { try { DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration(); configuration.setOfficeHome(officeHome); - String []portsString = serverPorts.split(","); + String[] portsString = serverPorts.split(","); int[] ports = Arrays.stream(portsString).mapToInt(Integer::parseInt).toArray(); @@ -86,8 +86,8 @@ public class OfficePluginManager { return converter; } - private Map getLoadProperties() { - Map loadProperties = new HashMap<>(10); + private Map getLoadProperties() { + Map loadProperties = new HashMap<>(10); loadProperties.put("Hidden", true); loadProperties.put("ReadOnly", true); loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE); @@ -97,9 +97,8 @@ public class OfficePluginManager { private boolean killProcess() { boolean flag = false; - Properties props = System.getProperties(); try { - if (props.getProperty("os.name").toLowerCase().contains("windows")) { + if (PlatformUtils.isWindows()) { Process p = Runtime.getRuntime().exec("cmd /c tasklist "); ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream os = p.getInputStream(); @@ -112,8 +111,22 @@ public class OfficePluginManager { Runtime.getRuntime().exec("taskkill /im " + "soffice.bin" + " /f"); flag = true; } + } else if (PlatformUtils.isLinux()) { + Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", "ps -ef | grep " + "soffice.bin" + " |grep -v grep | wc -l"}); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + InputStream os = p.getInputStream(); + byte[] b = new byte[256]; + while (os.read(b) > 0) { + baos.write(b); + } + String s = baos.toString(); + if (!"0".equals(s)) { + String[] cmd = {"sh", "-c", "ps -ef | grep soffice.bin | grep -v grep | awk '{print \"kill -9 \"$2}' | sh"}; + Runtime.getRuntime().exec(cmd); + flag = true; + } } else { - Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","ps -ef | grep " + "soffice.bin"}); + Process p = Runtime.getRuntime().exec(new String[]{"sh", "-c", "ps -ef | grep " + "soffice.bin"}); ByteArrayOutputStream baos = new ByteArrayOutputStream(); InputStream os = p.getInputStream(); byte[] b = new byte[256]; @@ -122,7 +135,7 @@ public class OfficePluginManager { } String s = baos.toString(); if (StringUtils.ordinalIndexOf(s, "soffice.bin", 3) > 0) { - String[] cmd ={"sh","-c","kill -15 `ps -ef|grep " + "soffice.bin" + "|awk 'NR==1{print $2}'`"}; + String[] cmd = {"sh", "-c", "kill -15 `ps -ef|grep " + "soffice.bin" + "|awk 'NR==1{print $2}'`"}; Runtime.getRuntime().exec(cmd); flag = true; } @@ -134,7 +147,7 @@ public class OfficePluginManager { } @PreDestroy - public void destroyOfficeManager(){ + public void destroyOfficeManager() { if (null != officeManager && officeManager.isRunning()) { logger.info("Shutting down office process"); officeManager.stop();