xiongzhu há 4 anos atrás
pai
commit
2379b5bfcc

+ 2 - 21
src/main/java/com/izouma/awesomeAdmin/web/Word2PDFController.java

@@ -20,13 +20,10 @@ import java.io.*;
 @Controller
 @Controller
 @Slf4j
 @Slf4j
 public class Word2PDFController {
 public class Word2PDFController {
-    private ActiveXComponent app       = null;
-    private Dispatch         documents = null;
 
 
     @PostMapping(value = "/word2pdf", produces = "application/pdf")
     @PostMapping(value = "/word2pdf", produces = "application/pdf")
     @ResponseBody
     @ResponseBody
     public byte[] word2pdf(@RequestParam("file") MultipartFile file) {
     public byte[] word2pdf(@RequestParam("file") MultipartFile file) {
-
         log.info("开始转换");
         log.info("开始转换");
         // 开始时间
         // 开始时间
         long start = System.currentTimeMillis();
         long start = System.currentTimeMillis();
@@ -35,6 +32,8 @@ public class Word2PDFController {
             File target = TempFile.createTempFile("word2pdf", ".pdf");
             File target = TempFile.createTempFile("word2pdf", ".pdf");
             FileUtils.write(file.getInputStream(), word);
             FileUtils.write(file.getInputStream(), word);
 
 
+            ActiveXComponent app = new ActiveXComponent("Word.Application");
+            Dispatch documents = app.getProperty("Documents").toDispatch();
             System.out.println("打开文件: " + word.getPath());
             System.out.println("打开文件: " + word.getPath());
             Dispatch document = Dispatch.call(documents, "Open", word.getPath(), false, true).toDispatch();
             Dispatch document = Dispatch.call(documents, "Open", word.getPath(), false, true).toDispatch();
             if (target.exists()) {
             if (target.exists()) {
@@ -51,23 +50,5 @@ public class Word2PDFController {
             log.error("转换失败", e);
             log.error("转换失败", e);
             throw new BusinessException("转换失败" + e.getMessage());
             throw new BusinessException("转换失败" + e.getMessage());
         }
         }
-
-    }
-
-    @PostConstruct
-    public void init() {
-        app = new ActiveXComponent("Word.Application");
-        documents = app.getProperty("Documents").toDispatch();
-    }
-
-    @PreDestroy
-    public void destroy() {
-        if (app != null) {
-            try {
-                app.invoke("Quit", 0);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
     }
     }
 }
 }