优化:重构大量代码,修复异常

This commit is contained in:
陈精华
2020-05-15 18:09:19 +08:00
committed by kl
parent 8a52450629
commit 180e7bcb8a
35 changed files with 464 additions and 699 deletions

View File

@ -9,15 +9,15 @@ import org.artofsolving.jodconverter.office.OfficeManager;
import org.artofsolving.jodconverter.office.OfficeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@ -37,11 +37,12 @@ public class ConverterUtils {
@PostConstruct
public void initOfficeManager() {
String officeHome = OfficeUtils.getDefaultOfficeHome().getAbsolutePath();
File officeHome;
officeHome = OfficeUtils.getDefaultOfficeHome();
if (officeHome == null) {
throw new RuntimeException("找不到office组件请确认'office.home'配置是否有误");
}
boolean killOffice = killProcess("soffice.bin");
boolean killOffice = killProcess();
if (killOffice) {
logger.warn("检测到有正在运行的office进程已自动结束该进程");
}
@ -72,11 +73,11 @@ public class ConverterUtils {
loadProperties.put("Hidden", true);
loadProperties.put("ReadOnly", true);
loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE);
loadProperties.put("CharacterSet", Charset.forName("UTF-8").name());
loadProperties.put("CharacterSet", StandardCharsets.UTF_8.name());
return loadProperties;
}
private boolean killProcess(String processName) {
private boolean killProcess() {
boolean flag = false;
Properties props = System.getProperties();
try {
@ -84,32 +85,28 @@ public class ConverterUtils {
Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream os = p.getInputStream();
byte b[] = new byte[256];
byte[] b = new byte[256];
while (os.read(b) > 0) {
baos.write(b);
}
String s = baos.toString();
if (s.indexOf(processName) >= 0) {
Runtime.getRuntime().exec("taskkill /im " + processName + " /f");
if (s.contains("soffice.bin")) {
Runtime.getRuntime().exec("taskkill /im " + "soffice.bin" + " /f");
flag = true;
} else {
flag = false;
}
} else {
Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","ps -ef | grep " + processName});
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];
byte[] b = new byte[256];
while (os.read(b) > 0) {
baos.write(b);
}
String s = baos.toString();
if (StringUtils.ordinalIndexOf(s, processName, 3) > 0) {
String[] cmd ={"sh","-c","kill -15 `ps -ef|grep " + processName + "|awk 'NR==1{print $2}'`"};
if (StringUtils.ordinalIndexOf(s, "soffice.bin", 3) > 0) {
String[] cmd ={"sh","-c","kill -15 `ps -ef|grep " + "soffice.bin" + "|awk 'NR==1{print $2}'`"};
Runtime.getRuntime().exec(cmd);
flag = true;
} else {
flag = false;
}
}
} catch (IOException e) {