|
@@ -1,131 +1,131 @@
|
|
|
-package com.management.platform.util;
|
|
|
-
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.artofsolving.jodconverter.OfficeDocumentConverter;
|
|
|
-import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
|
|
|
-import org.artofsolving.jodconverter.office.OfficeManager;
|
|
|
-
|
|
|
-import java.io.File;
|
|
|
-import java.io.IOException;
|
|
|
-import java.util.Random;
|
|
|
-import java.util.regex.Pattern;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-public class OpenOfficeService {
|
|
|
- //64位windows系统默认安装路径
|
|
|
- public static String OpenOffice_HOME_64 = "C://Program Files (x86)/OpenOffice 4/";
|
|
|
- //32位windows系统默认安装路径
|
|
|
- public static String OpenOffice_HOME_32 = "C://Program Files/OpenOffice 4/";
|
|
|
- public static String OpenOffice_HOME_UNIX = "/opt/openoffice4/";
|
|
|
- public static int PORT = 12312;
|
|
|
- private static OfficeManager officeManager;
|
|
|
-
|
|
|
- public static boolean canTransferToPdf(String suffix) {
|
|
|
- if (".doc".equals(suffix) || ".docx".equals(suffix)
|
|
|
- || ".xls".equals(suffix) || ".xlsx".equals(suffix)
|
|
|
- || ".ppt".equals(suffix) || ".pptx".equals(suffix)
|
|
|
- || ".jpg".equals(suffix) || ".png".equals(suffix)
|
|
|
- || ".bmp".equals(suffix) || ".jpeg".equals(suffix)
|
|
|
- || ".txt".equals(suffix)) {
|
|
|
- return true;
|
|
|
- } else {
|
|
|
- return false;
|
|
|
- }
|
|
|
- }
|
|
|
- /**
|
|
|
- * 启动openoffice服务
|
|
|
- */
|
|
|
- public void start() {
|
|
|
- try {
|
|
|
- log.info("===Openoffice实例启动中...");
|
|
|
- DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
|
|
|
- String officeHome = getOfficeHome();
|
|
|
- log.info("officeHome====="+officeHome);
|
|
|
- config.setOfficeHome(officeHome);
|
|
|
-// int randomPort = new Random().nextInt(1000);
|
|
|
-// randomPort += 8000;
|
|
|
- config.setPortNumber(PORT);
|
|
|
- officeManager = config.buildOfficeManager();
|
|
|
- officeManager.start();
|
|
|
- log.info("Openoffice实例启动成功!");
|
|
|
- } catch (Exception e) {
|
|
|
- e.printStackTrace();
|
|
|
- log.info("OpenOffice启动失败:"+e.getMessage());
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static String getOfficeHome() {
|
|
|
- String osName = System.getProperty("os.name");
|
|
|
- if (Pattern.matches("Linux.*", osName)) {
|
|
|
- return OpenOffice_HOME_UNIX;
|
|
|
- } else if (Pattern.matches("Windows.*", osName)) {
|
|
|
- String arch = System.getProperty("os.arch");
|
|
|
- boolean is64bit = (System.getenv("ProgramFiles(x86)") != null);
|
|
|
- if (is64bit) {
|
|
|
- return OpenOffice_HOME_64;
|
|
|
- } else {
|
|
|
- return OpenOffice_HOME_32;
|
|
|
- }
|
|
|
- } else if (Pattern.matches("Mac.*", osName)) {
|
|
|
- return "/Application/OpenOffice.org.app/Contents";
|
|
|
- }
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 文档转换
|
|
|
- * @param inputFilePath
|
|
|
- * @param destFile
|
|
|
- * @return 0-转换成,-1转换失败
|
|
|
- */
|
|
|
- public int office2PDF(String inputFilePath, String destFile) {
|
|
|
- //txt后缀需要改成odt再进行转换
|
|
|
- if (inputFilePath.endsWith(".txt")) {
|
|
|
- String tempFilePath = inputFilePath.replaceAll(".txt", ".odt");
|
|
|
- try {
|
|
|
- org.apache.commons.io.FileUtils.copyFile(new File(inputFilePath), new File(tempFilePath));
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
- inputFilePath = tempFilePath;
|
|
|
- }
|
|
|
- OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
|
|
|
- File outputFile = new File(destFile);
|
|
|
- if (!outputFile.getParentFile().exists()) {
|
|
|
- outputFile.getParentFile().mkdirs();
|
|
|
- }
|
|
|
- File inputFile = new File(inputFilePath);
|
|
|
- if (inputFile.exists()) {// 找不到源文件, 则返回
|
|
|
- converter.convert(inputFile, outputFile);
|
|
|
- } else {
|
|
|
- return -1;
|
|
|
- }
|
|
|
- return 0;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 停止服务
|
|
|
- */
|
|
|
- public void shutdown() {
|
|
|
- if (officeManager != null) {
|
|
|
- officeManager.stop();
|
|
|
- System.out.println("Openoffice实例停止!");
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public static void main(String[] args) {
|
|
|
- OpenOfficeService service = new OpenOfficeService();
|
|
|
- String sFile = "D:\\1.pdf";
|
|
|
- String dFile = "D:\\wttxls111000000000000000.pdf";
|
|
|
- service.start();
|
|
|
- File testFile = new File(dFile);
|
|
|
- if(!testFile .exists()){
|
|
|
- System.out.println("测试文件不存在,开始转换......");
|
|
|
- service.office2PDF(sFile,dFile);
|
|
|
- }else{
|
|
|
- System.out.println("测试文件已存在");
|
|
|
- }
|
|
|
- service.shutdown();
|
|
|
- }
|
|
|
-}
|
|
|
+//package com.management.platform.util;
|
|
|
+//
|
|
|
+//import lombok.extern.slf4j.Slf4j;
|
|
|
+//import org.artofsolving.jodconverter.OfficeDocumentConverter;
|
|
|
+//import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
|
|
|
+//import org.artofsolving.jodconverter.office.OfficeManager;
|
|
|
+//
|
|
|
+//import java.io.File;
|
|
|
+//import java.io.IOException;
|
|
|
+//import java.util.Random;
|
|
|
+//import java.util.regex.Pattern;
|
|
|
+//
|
|
|
+//@Slf4j
|
|
|
+//public class OpenOfficeService {
|
|
|
+// //64位windows系统默认安装路径
|
|
|
+// public static String OpenOffice_HOME_64 = "C://Program Files (x86)/OpenOffice 4/";
|
|
|
+// //32位windows系统默认安装路径
|
|
|
+// public static String OpenOffice_HOME_32 = "C://Program Files/OpenOffice 4/";
|
|
|
+// public static String OpenOffice_HOME_UNIX = "/opt/openoffice4/";
|
|
|
+// public static int PORT = 12312;
|
|
|
+// private static OfficeManager officeManager;
|
|
|
+//
|
|
|
+// public static boolean canTransferToPdf(String suffix) {
|
|
|
+// if (".doc".equals(suffix) || ".docx".equals(suffix)
|
|
|
+// || ".xls".equals(suffix) || ".xlsx".equals(suffix)
|
|
|
+// || ".ppt".equals(suffix) || ".pptx".equals(suffix)
|
|
|
+// || ".jpg".equals(suffix) || ".png".equals(suffix)
|
|
|
+// || ".bmp".equals(suffix) || ".jpeg".equals(suffix)
|
|
|
+// || ".txt".equals(suffix)) {
|
|
|
+// return true;
|
|
|
+// } else {
|
|
|
+// return false;
|
|
|
+// }
|
|
|
+// }
|
|
|
+// /**
|
|
|
+// * 启动openoffice服务
|
|
|
+// */
|
|
|
+// public void start() {
|
|
|
+// try {
|
|
|
+// log.info("===Openoffice实例启动中...");
|
|
|
+// DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration();
|
|
|
+// String officeHome = getOfficeHome();
|
|
|
+// log.info("officeHome====="+officeHome);
|
|
|
+// config.setOfficeHome(officeHome);
|
|
|
+//// int randomPort = new Random().nextInt(1000);
|
|
|
+//// randomPort += 8000;
|
|
|
+// config.setPortNumber(PORT);
|
|
|
+// officeManager = config.buildOfficeManager();
|
|
|
+// officeManager.start();
|
|
|
+// log.info("Openoffice实例启动成功!");
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// log.info("OpenOffice启动失败:"+e.getMessage());
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// public static String getOfficeHome() {
|
|
|
+// String osName = System.getProperty("os.name");
|
|
|
+// if (Pattern.matches("Linux.*", osName)) {
|
|
|
+// return OpenOffice_HOME_UNIX;
|
|
|
+// } else if (Pattern.matches("Windows.*", osName)) {
|
|
|
+// String arch = System.getProperty("os.arch");
|
|
|
+// boolean is64bit = (System.getenv("ProgramFiles(x86)") != null);
|
|
|
+// if (is64bit) {
|
|
|
+// return OpenOffice_HOME_64;
|
|
|
+// } else {
|
|
|
+// return OpenOffice_HOME_32;
|
|
|
+// }
|
|
|
+// } else if (Pattern.matches("Mac.*", osName)) {
|
|
|
+// return "/Application/OpenOffice.org.app/Contents";
|
|
|
+// }
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 文档转换
|
|
|
+// * @param inputFilePath
|
|
|
+// * @param destFile
|
|
|
+// * @return 0-转换成,-1转换失败
|
|
|
+// */
|
|
|
+// public int office2PDF(String inputFilePath, String destFile) {
|
|
|
+// //txt后缀需要改成odt再进行转换
|
|
|
+// if (inputFilePath.endsWith(".txt")) {
|
|
|
+// String tempFilePath = inputFilePath.replaceAll(".txt", ".odt");
|
|
|
+// try {
|
|
|
+// org.apache.commons.io.FileUtils.copyFile(new File(inputFilePath), new File(tempFilePath));
|
|
|
+// } catch (IOException e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// inputFilePath = tempFilePath;
|
|
|
+// }
|
|
|
+// OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
|
|
|
+// File outputFile = new File(destFile);
|
|
|
+// if (!outputFile.getParentFile().exists()) {
|
|
|
+// outputFile.getParentFile().mkdirs();
|
|
|
+// }
|
|
|
+// File inputFile = new File(inputFilePath);
|
|
|
+// if (inputFile.exists()) {// 找不到源文件, 则返回
|
|
|
+// converter.convert(inputFile, outputFile);
|
|
|
+// } else {
|
|
|
+// return -1;
|
|
|
+// }
|
|
|
+// return 0;
|
|
|
+// }
|
|
|
+//
|
|
|
+// /**
|
|
|
+// * 停止服务
|
|
|
+// */
|
|
|
+// public void shutdown() {
|
|
|
+// if (officeManager != null) {
|
|
|
+// officeManager.stop();
|
|
|
+// System.out.println("Openoffice实例停止!");
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+//
|
|
|
+// public static void main(String[] args) {
|
|
|
+// OpenOfficeService service = new OpenOfficeService();
|
|
|
+// String sFile = "D:\\1.pdf";
|
|
|
+// String dFile = "D:\\wttxls111000000000000000.pdf";
|
|
|
+// service.start();
|
|
|
+// File testFile = new File(dFile);
|
|
|
+// if(!testFile .exists()){
|
|
|
+// System.out.println("测试文件不存在,开始转换......");
|
|
|
+// service.office2PDF(sFile,dFile);
|
|
|
+// }else{
|
|
|
+// System.out.println("测试文件已存在");
|
|
|
+// }
|
|
|
+// service.shutdown();
|
|
|
+// }
|
|
|
+//}
|