|
@@ -1,14 +1,19 @@
|
|
|
package com.kingtom.office.controller;
|
|
|
|
|
|
import java.io.*;
|
|
|
+import java.text.MessageFormat;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
+
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
import com.kingtom.office.config.AppConfig;
|
|
|
-import jakarta.servlet.http.HttpServletResponse;
|
|
|
-import org.jodconverter.core.DocumentConverter;
|
|
|
-import org.jodconverter.core.office.OfficeException;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.exec.CommandLine;
|
|
|
+import org.apache.commons.exec.DefaultExecutor;
|
|
|
+import org.apache.commons.exec.ExecuteException;
|
|
|
+import org.apache.commons.exec.ExecuteResultHandler;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Controller;
|
|
|
import org.springframework.util.StringUtils;
|
|
@@ -29,11 +34,9 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
*/
|
|
|
@Controller
|
|
|
@RequestMapping("office")
|
|
|
+@Slf4j
|
|
|
public class OfficeController{
|
|
|
|
|
|
- @Autowired
|
|
|
- private DocumentConverter converter;
|
|
|
-
|
|
|
@Autowired
|
|
|
private AppConfig appConfig;
|
|
|
|
|
@@ -42,16 +45,18 @@ public class OfficeController{
|
|
|
|
|
|
@PostMapping("toPdf")
|
|
|
public void toPdf(@RequestParam("file") MultipartFile file, @RequestParam("fileType") String fileType,
|
|
|
- HttpServletResponse response) throws IOException, OfficeException{
|
|
|
- String uuid = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
|
|
|
-
|
|
|
- File srcFile = new File(getFilePath() + uuid + fileType);
|
|
|
- FileUtil.copyFile(file.getInputStream(), srcFile);
|
|
|
-
|
|
|
- String fileName = uuid + ".pdf";
|
|
|
- File targetFile = new File(getFilePath() + fileName);
|
|
|
- converter.convert(srcFile).to(targetFile).execute();
|
|
|
- output(response, fileName, targetFile);
|
|
|
+ HttpServletResponse response) throws Exception{
|
|
|
+ try{
|
|
|
+ String uuid = StringUtils.replace(UUID.randomUUID().toString(), "-", "");
|
|
|
+ File srcFile = new File(getFilePath() + uuid + "." + fileType);
|
|
|
+ FileUtil.copyFile(file.getInputStream(), srcFile);
|
|
|
+ File targetFile = targetFile = libreOfficeConvert(srcFile);
|
|
|
+ //File targetFile = asposeConvert(srcFile);
|
|
|
+ output(response, targetFile.getName(), targetFile);
|
|
|
+ } catch(Exception e){
|
|
|
+ log.error(e.getMessage(), e);
|
|
|
+ throw new Exception("转换失败!");
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
private static void output(HttpServletResponse response, String fileName, File file) throws IOException{
|
|
@@ -81,4 +86,59 @@ public class OfficeController{
|
|
|
private String getFilePath(){
|
|
|
return appConfig.getFileHome() + DateUtil.format(DateUtil.date(), FORMAT);
|
|
|
}
|
|
|
+
|
|
|
+ private synchronized File libreOfficeConvert(File officeFile) throws IOException, InterruptedException{
|
|
|
+ DefaultExecutor exec = new DefaultExecutor();
|
|
|
+ ExecuteResultHandler erh = new ExecuteResultHandler(){
|
|
|
+ @Override
|
|
|
+ public void onProcessComplete(int exitValue){
|
|
|
+ log.info(" 转换完成! Exit value: {}", exitValue);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onProcessFailed(ExecuteException e){
|
|
|
+ log.error(" 转换失败!", e);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ File tempFolder = new File(getFilePath());
|
|
|
+ String osName = System.getProperty("os.name");
|
|
|
+ String command = null;
|
|
|
+ if(osName.contains("Windows")){
|
|
|
+ command = "cmd /c start soffice --headless --invisible --convert-to pdf:writer_pdf_Export \"{0}\" "
|
|
|
+ + "--outdir \"{1}\"";
|
|
|
+ } else{
|
|
|
+ String fileType = officeFile.getName().substring(officeFile.getName().lastIndexOf("."));
|
|
|
+ String cmd;
|
|
|
+ switch(fileType){
|
|
|
+ case ".docx":
|
|
|
+ case ".doc":
|
|
|
+ cmd = "writer";
|
|
|
+ break;
|
|
|
+ case ".ppt":
|
|
|
+ case ".pptx":
|
|
|
+ cmd = "impress";
|
|
|
+ break;
|
|
|
+ case ".xls":
|
|
|
+ case ".xlsx":
|
|
|
+ cmd = "calc";
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ throw new RuntimeException("不支持的文件类型");
|
|
|
+ }
|
|
|
+ command = "libreoffice --headless --invisible --convert-to "
|
|
|
+ + "pdf:writer_pdf_Export \"{0}\" --outdir \"{1}\"";
|
|
|
+ }
|
|
|
+ command = MessageFormat.format(command, officeFile.getAbsolutePath(), tempFolder.getAbsolutePath());
|
|
|
+ log.info("执行office文件转换任务,命令为{}", command);
|
|
|
+ exec.execute(CommandLine.parse(command), erh);
|
|
|
+ File outputFile = new File(tempFolder.getAbsolutePath() + File.separator + officeFile.getName()
|
|
|
+ .substring(0, officeFile.getName().indexOf(".")) + ".pdf");
|
|
|
+ //CountDownLatch latch = new CountDownLatch(1);不生效,所以换成了循环睡眠
|
|
|
+ int time = 0;
|
|
|
+ while((!outputFile.exists()) && time < appConfig.getWaitTime()){
|
|
|
+ Thread.sleep(1000);
|
|
|
+ time++;
|
|
|
+ }
|
|
|
+ return outputFile;
|
|
|
+ }
|
|
|
}
|