FileConversionUtil.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package com.ims.eval.util;
  2. import cn.hutool.core.date.DateUtil;
  3. import cn.hutool.core.io.FileTypeUtil;
  4. import com.artofsolving.jodconverter.DocumentConverter;
  5. import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
  6. import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
  7. import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
  8. import java.io.*;
  9. import java.nio.file.Files;
  10. import java.util.ArrayList;
  11. import java.util.Date;
  12. import java.util.List;
  13. /**
  14. * @author hlf
  15. * @date 2023/6/1 17:30
  16. * 文件说明:
  17. */
  18. public class FileConversionUtil {
  19. //启动OpenOffice服务
  20. static {
  21. List<String> command = new ArrayList<>();
  22. //OpenOffice的安装目录下的soffice路径D:\OpenOffice 4\program\soffice.exe
  23. command.add("D:\\OpenOffice 4\\program\\soffice.exe");
  24. command.add("-headless");
  25. command.add("-accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard");
  26. command.add("-nofirststartwizard");
  27. command.forEach(v -> System.out.print(v + " "));
  28. System.out.println();
  29. ProcessBuilder builder = new ProcessBuilder();
  30. //正常信息和错误信息合并输出
  31. builder.redirectErrorStream(true);
  32. builder.command(command);
  33. //开始执行命令
  34. try {
  35. builder.start();
  36. } catch (IOException e) {
  37. e.printStackTrace();
  38. }
  39. }
  40. /**
  41. * office文件转换成pdf文件
  42. *
  43. * @param fromFilePath 要转换文件的文件路径
  44. * @param saveFilePath 转换完后文件的保存路径
  45. * @return 返回最后转换后的文件名
  46. */
  47. public static String conversionToPdf(String fromFilePath, String saveFilePath) {
  48. String timesuffix = DateUtil.format(new Date(), "yyyyMMddHHmmss");
  49. String docFileName;
  50. String resultFileName;
  51. // 识别文件类型
  52. String fileType = "";
  53. try (FileInputStream fromFileInputStream = new FileInputStream(fromFilePath)) {
  54. fileType = FileTypeUtil.getType(fromFileInputStream);
  55. } catch (FileNotFoundException e) {
  56. return "待转换的文件不存在";
  57. } catch (IOException e) {
  58. return "文件读取失败";
  59. }
  60. if ("doc".equals(fileType)) {
  61. docFileName = "doc_" + timesuffix + ".doc";
  62. resultFileName = "doc_" + timesuffix + ".pdf";
  63. } else if ("docx".equals(fileType)) {
  64. docFileName = "docx_" + timesuffix + ".docx";
  65. resultFileName = "docx_" + timesuffix + ".pdf";
  66. } else if ("xls".equals(fileType)) {
  67. docFileName = "xls_" + timesuffix + ".xls";
  68. resultFileName = "xls_" + timesuffix + ".pdf";
  69. } else if ("xlsx".equals(fileType)) {
  70. docFileName = "xlsx_" + timesuffix + ".xlsx";
  71. resultFileName = "xlsx_" + timesuffix + ".pdf";
  72. } else if ("ppt".equals(fileType)) {
  73. docFileName = "ppt_" + timesuffix + ".ppt";
  74. resultFileName = "ppt_" + timesuffix + ".pdf";
  75. } else if ("pptx".equals(fileType)) {
  76. docFileName = "pptx_" + timesuffix + ".pptx";
  77. resultFileName = "pptx_" + timesuffix + ".pdf";
  78. } else {
  79. return "转换错误,文件后缀不是doc、docx、xls、xlsx、ppt、pptx";
  80. }
  81. // 将待转文件拷贝一份写入到saveFilePath下
  82. File docInputFile = new File(saveFilePath + File.separatorChar + docFileName);
  83. File resultOutputFile = new File(saveFilePath + File.separatorChar + resultFileName);
  84. if (resultOutputFile.exists()) {
  85. resultOutputFile.delete();
  86. }
  87. try (OutputStream os = Files.newOutputStream(docInputFile.toPath())) {
  88. try (FileInputStream fromFileInputStream = new FileInputStream(fromFilePath)) {
  89. int bytesRead = 0;
  90. byte[] buffer = new byte[1024 * 8];
  91. while ((bytesRead = fromFileInputStream.read(buffer)) != -1) {
  92. os.write(buffer, 0, bytesRead);
  93. }
  94. }
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. // 连接OpenOffice服务。需提前开启OpenOffice服务,否则会报错
  99. OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
  100. try {
  101. connection.connect();
  102. } catch (Exception e) {
  103. System.out.println("连接OpenOffice服务失败,请检查是否启动OpenOffice服务");
  104. }
  105. // 转化,将saveFilePath下的拷贝的原始文件转化为pdf
  106. DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
  107. try {
  108. converter.convert(docInputFile, resultOutputFile);
  109. } catch (Exception e) {
  110. e.printStackTrace();
  111. }
  112. // 转换完之后删除拷贝的原始文件
  113. docInputFile.delete();
  114. connection.disconnect();
  115. return resultFileName;
  116. }
  117. }