ConverterUtils.java 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package cn.keking.utils;
  2. import com.sun.star.document.UpdateDocMode;
  3. import cn.keking.extend.ControlDocumentFormatRegistry;
  4. import org.apache.commons.lang3.StringUtils;
  5. import org.artofsolving.jodconverter.OfficeDocumentConverter;
  6. import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
  7. import org.artofsolving.jodconverter.office.OfficeManager;
  8. import org.artofsolving.jodconverter.office.OfficeUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Value;
  12. import org.springframework.stereotype.Component;
  13. import javax.annotation.PostConstruct;
  14. import javax.annotation.PreDestroy;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.nio.charset.Charset;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import java.util.Properties;
  22. /**
  23. * 创建文件转换器
  24. *
  25. * @author yudian-it
  26. * @date 2017/11/13
  27. */
  28. @Component
  29. public class ConverterUtils {
  30. private final Logger logger = LoggerFactory.getLogger(ConverterUtils.class);
  31. // @Value("${office.home}")
  32. // String officeHome;
  33. // OpenOfficeConnection connection;
  34. OfficeManager officeManager;
  35. @PostConstruct
  36. public void initOfficeManager() {
  37. String officeHome = OfficeUtils.getDefaultOfficeHome().getAbsolutePath();
  38. if (officeHome == null) {
  39. throw new RuntimeException("找不到office组件,请确认'office.home'配置是否有误");
  40. }
  41. boolean killOffice = killProcess("soffice.bin");
  42. if (killOffice) {
  43. logger.warn("检测到有正在运行的office进程,已自动结束该进程");
  44. }
  45. try {
  46. DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
  47. configuration.setOfficeHome(officeHome);
  48. configuration.setPortNumber(8100);
  49. // 设置任务执行超时为5分钟
  50. configuration.setTaskExecutionTimeout(1000 * 60 * 5L);
  51. // 设置任务队列超时为24小时
  52. //configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);
  53. officeManager = configuration.buildOfficeManager();
  54. officeManager.start();
  55. } catch (Exception e) {
  56. logger.error("启动office组件失败,请检查office组件是否可用");
  57. throw e;
  58. }
  59. }
  60. public OfficeDocumentConverter getDocumentConverter() {
  61. OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager, new ControlDocumentFormatRegistry());
  62. converter.setDefaultLoadProperties(getLoadProperties());
  63. return converter;
  64. }
  65. private Map<String,?> getLoadProperties() {
  66. Map<String,Object> loadProperties = new HashMap<>(10);
  67. loadProperties.put("Hidden", true);
  68. loadProperties.put("ReadOnly", true);
  69. loadProperties.put("UpdateDocMode", UpdateDocMode.QUIET_UPDATE);
  70. loadProperties.put("CharacterSet", Charset.forName("UTF-8").name());
  71. return loadProperties;
  72. }
  73. private boolean killProcess(String processName) {
  74. boolean flag = false;
  75. Properties props = System.getProperties();
  76. try {
  77. if (props.getProperty("os.name").toLowerCase().contains("windows")) {
  78. Process p = Runtime.getRuntime().exec("cmd /c tasklist ");
  79. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  80. InputStream os = p.getInputStream();
  81. byte b[] = new byte[256];
  82. while (os.read(b) > 0) {
  83. baos.write(b);
  84. }
  85. String s = baos.toString();
  86. if (s.indexOf(processName) >= 0) {
  87. Runtime.getRuntime().exec("taskkill /im " + processName + " /f");
  88. flag = true;
  89. } else {
  90. flag = false;
  91. }
  92. } else {
  93. Process p = Runtime.getRuntime().exec(new String[]{"sh","-c","ps -ef | grep " + processName});
  94. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  95. InputStream os = p.getInputStream();
  96. byte b[] = new byte[256];
  97. while (os.read(b) > 0) {
  98. baos.write(b);
  99. }
  100. String s = baos.toString();
  101. if (StringUtils.ordinalIndexOf(s, processName, 3) > 0) {
  102. String[] cmd ={"sh","-c","kill -15 `ps -ef|grep " + processName + "|awk 'NR==1{print $2}'`"};
  103. Runtime.getRuntime().exec(cmd);
  104. flag = true;
  105. } else {
  106. flag = false;
  107. }
  108. }
  109. } catch (IOException e) {
  110. logger.error("检测office进程异常", e);
  111. }
  112. return flag;
  113. }
  114. @PreDestroy
  115. public void destroyOfficeManager(){
  116. if (null != officeManager && officeManager.isRunning()) {
  117. officeManager.stop();
  118. }
  119. }
  120. }