Selaa lähdekoodia

系统资源配置

wanxing 2 kuukautta sitten
vanhempi
commit
4840a3250e

+ 20 - 9
pom.xml

@@ -156,19 +156,24 @@
 
         <!-- openOffice begin-->
         <dependency>
-            <groupId>org.jodconverter</groupId>
-            <artifactId>jodconverter-core</artifactId>
-            <version>4.2.2</version>
+            <groupId>com.artofsolving</groupId>
+            <artifactId>jodconverter</artifactId>
+            <version>2.2.2</version>
         </dependency>
         <dependency>
-            <groupId>org.jodconverter</groupId>
-            <artifactId>jodconverter-spring-boot-starter</artifactId>
-            <version>4.2.2</version>
+            <groupId>org.openoffice</groupId>
+            <artifactId>juh</artifactId>
+            <version>4.1.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.openoffice</groupId>
+            <artifactId>ridl</artifactId>
+            <version>4.1.2</version>
         </dependency>
         <dependency>
-            <groupId>org.jodconverter</groupId>
-            <artifactId>jodconverter-local</artifactId>
-            <version>4.2.2</version>
+            <groupId>org.openoffice</groupId>
+            <artifactId>unoil</artifactId>
+            <version>4.1.2</version>
         </dependency>
         <!--openOffice end-->
 
@@ -282,6 +287,12 @@
         </dependency>
         <!-- htmlToPDF end -->
 
+        <dependency>
+            <groupId>com.google.code.gson</groupId>
+            <artifactId>gson</artifactId>
+            <version>2.10.1</version>
+        </dependency>
+
 
     </dependencies>
 

+ 3 - 8
src/main/java/com/singularity/controller/SubjectiveEvaluationController.java

@@ -29,12 +29,10 @@ import com.singularity.pojo.vo.subjectiveEvaluation.QueryEvaluatorNameListVO;
 import com.singularity.pojo.vo.template.QueryAttachmentListVO;
 import com.singularity.exception.ToolsBusinessWarn;
 import com.singularity.util.FileUtil;
-import com.singularity.util.OpenOfficeUtil;
 import com.singularity.util.result.CodeMessage;
 import com.singularity.util.result.Result;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
-import org.jodconverter.office.OfficeException;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 
@@ -59,9 +57,6 @@ public class SubjectiveEvaluationController {
     @Resource
     private AttachmentService attachmentService;
 
-    @Resource
-    private OpenOfficeUtil openOfficeUtil;
-
     @Resource
     private CarColumnService carColumnService;
 
@@ -195,9 +190,9 @@ public class SubjectiveEvaluationController {
             response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
             InputStream inputStream = attachmentService.downloadAttachment(attachmentById.getObjName());
             if ("docx".equals(type) || "doc".equals(type)) {
-                openOfficeUtil.wordToPDF(inputStream, response.getOutputStream());
+//                openOfficeUtil.wordToPDF(inputStream, response.getOutputStream());
             } else if ("xlsx".equals(type) || "xls".equals(type)) {
-                openOfficeUtil.excelToPDF(openOfficeUtil.refresh(inputStream,type), response.getOutputStream());
+//                openOfficeUtil.excelToPDF(openOfficeUtil.refresh(inputStream,type), response.getOutputStream());
             } else if("pdf".equals(type)){
                 BufferedInputStream bis = new BufferedInputStream(inputStream);
                 BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
@@ -211,7 +206,7 @@ public class SubjectiveEvaluationController {
             }else {
                 throw new SaTokenException("文件格式错误");
             }
-        } catch (IOException | OfficeException e) {
+        } catch (IOException e) {
             throw new SaTokenException("预览失败");
         }
     }

+ 257 - 0
src/main/java/com/singularity/util/FileConvertUtil.java

@@ -0,0 +1,257 @@
+package com.singularity.util;
+
+import com.artofsolving.jodconverter.DefaultDocumentFormatRegistry;
+import com.artofsolving.jodconverter.DocumentConverter;
+import com.artofsolving.jodconverter.DocumentFormat;
+import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
+import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
+import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.springframework.beans.factory.annotation.Value;
+
+import java.io.*;
+import java.net.ConnectException;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.nio.file.Files;
+import java.util.UUID;
+
+/**
+ * 文件格式转换工具类
+ */
+public class FileConvertUtil {
+    /**
+     * 默认转换后文件后缀
+     */
+    private static final String DEFAULT_SUFFIX = "pdf";
+    /**
+     * openoffice的host:你部署openoffice的服务器ip
+     */
+    private static final String OPENOFFICE_HOST = "10.148.111.13";
+    /**
+     * openoffice的port
+     */
+    private static final Integer OPENOFFICE_PORT = 8100;
+
+    @Value("${file.path}")
+    private String filePath;
+
+    public static void test(){
+        OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_HOST, OPENOFFICE_PORT);
+        try {
+            connection.connect();
+        } catch (ConnectException e) {
+            throw new RuntimeException(e);
+        }
+    }
+    /**
+     * 方法描述 office文档转换为PDF(处理本地文件)
+     * @param sourcePath 源文件路径
+     * @param suffix     源文件后缀
+     * @return InputStream 转换后文件输入流
+     */
+    public static InputStream convertLocaleFile(String sourcePath, String suffix) throws Exception {
+        File inputFile = new File(sourcePath);
+        InputStream inputStream = new FileInputStream(inputFile);
+        return covertCommonByStream(inputStream, suffix);
+    }
+
+    /**
+     * office文档转换为PDF文件流(处理网络文件)
+     *
+     * @param netFileUrl 网络文件路径
+     * @param suffix     文件后缀
+     * @return InputStream 转换后文件输入流
+     */
+    public static InputStream convertNetFile(String netFileUrl, String suffix) throws Exception {
+        // 创建URL
+        URL url = new URL(netFileUrl);
+        // 试图连接并取得返回状态码
+        URLConnection urlconn = url.openConnection();
+        urlconn.connect();
+        HttpURLConnection httpconn = (HttpURLConnection) urlconn;
+        int httpResult = httpconn.getResponseCode();
+        if (httpResult == HttpURLConnection.HTTP_OK) {
+            InputStream inputStream = urlconn.getInputStream();
+            return covertCommonByStream(inputStream, suffix);
+        }
+        return null;
+    }
+
+    /**
+     * office文档转换为PDF文件(处理网络文件)
+     *
+     * @param netFileUrl 网络文件路径
+     * @param suffix     文件后缀
+     * @param targetPath      目标文件全路径 eg: C:\Users\cvec2022\Desktop\abc.pdf
+     * @return InputStream 转换后文件输入流
+     */
+    public static File convertNetFileToFile(String netFileUrl, String suffix, String targetPath) throws Exception {
+        // 创建URL
+        URL url = new URL(netFileUrl);
+        // 试图连接并取得返回状态码
+        URLConnection urlconn = url.openConnection();
+        urlconn.connect();
+        HttpURLConnection httpconn = (HttpURLConnection) urlconn;
+        int httpResult = httpconn.getResponseCode();
+        if (httpResult == HttpURLConnection.HTTP_OK) {
+            InputStream inputStream = urlconn.getInputStream();
+            return covertCommonByStream(inputStream, suffix, targetPath);
+        }
+        return null;
+    }
+
+    /**
+     * 将文件以流的形式转换
+     *
+     * @param inputStream 源文件输入流
+     * @param suffix      源文件后缀
+     * @return InputStream 转换后文件输入流
+     */
+    public static InputStream covertCommonByStream(InputStream inputStream, String suffix) throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_HOST, OPENOFFICE_PORT);
+        connection.connect();
+        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
+        DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
+        DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX);
+        DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix);
+        converter.convert(inputStream, sourceFormat, out, targetFormat);
+        connection.disconnect();
+        return outputStreamConvertInputStream(out);
+    }
+
+    /**
+     * 将文件以文件的形式转换
+     *
+     * @param inputStream 源文件输入流
+     * @param suffix      源文件后缀
+     * @param targetPath      目标文件全路径 eg: C:\Users\cvec2022\Desktop\abc.pdf
+     * @return File 转换后文件
+     */
+    public static File covertCommonByStream(InputStream inputStream, String suffix, String targetPath) throws Exception {
+        ByteArrayOutputStream out = new ByteArrayOutputStream();
+        OpenOfficeConnection connection = new SocketOpenOfficeConnection(OPENOFFICE_HOST, OPENOFFICE_PORT);
+        connection.connect();
+        DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
+        DefaultDocumentFormatRegistry formatReg = new DefaultDocumentFormatRegistry();
+        DocumentFormat targetFormat = formatReg.getFormatByFileExtension(DEFAULT_SUFFIX);
+        DocumentFormat sourceFormat = formatReg.getFormatByFileExtension(suffix);
+        converter.convert(inputStream, sourceFormat, out, targetFormat);
+        connection.disconnect();
+        ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
+        return byteArrayToFile(baos.toByteArray(), targetPath);
+    }
+
+    /**
+     * byte数组转File
+     * @param byteArray 字节数组
+     * @param targetPath 目标路径
+     */
+    public static File byteArrayToFile(byte[] byteArray, String targetPath) {
+        InputStream in = new ByteArrayInputStream(byteArray);
+        File file = new File(targetPath);
+        String path = targetPath.substring(0, targetPath.lastIndexOf(File.separator));
+        if (!file.exists()) {
+            new File(path).mkdir();
+        }
+        FileOutputStream fos = null;
+        try {
+            fos = new FileOutputStream(file);
+            int len = 0;
+            byte[] buf = new byte[1024];
+            while ((len = in.read(buf)) != -1) {
+                fos.write(buf, 0, len);
+            }
+            fos.flush();
+        } catch (Exception e) {
+            e.printStackTrace();
+        } finally {
+            if (null != fos) {
+                try {
+                    fos.close();
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+        return file;
+    }
+
+    /**
+     *  inputStream转File
+     */
+    public static void inputStreamToFile(InputStream ins, File file) throws IOException {
+        OutputStream os = Files.newOutputStream(file.toPath());
+        int bytesRead = 0;
+        byte[] buffer = new byte[8192];
+        while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
+            os.write(buffer, 0, bytesRead);
+        }
+        os.close();
+        ins.close();
+    }
+
+    /**
+     *  outputStream转inputStream
+     */
+    public static ByteArrayInputStream outputStreamConvertInputStream(final OutputStream out) throws Exception {
+        ByteArrayOutputStream baos = (ByteArrayOutputStream) out;
+        return new ByteArrayInputStream(baos.toByteArray());
+    }
+
+    /**
+     * excel超范围预处理
+     */
+    public void refresh(String oldFilePath, String oldType, String newFilePath) throws IOException {
+        Workbook workbook = null;
+        if (oldType.equals("xls")) {
+            workbook = new HSSFWorkbook(new FileInputStream(oldFilePath));
+        } else if (oldType.equals("xlsx")) {
+            workbook = new XSSFWorkbook(new FileInputStream(oldFilePath));
+        }
+        assert workbook != null;
+        int sheetCount = workbook.getNumberOfSheets();
+        for (int i = 0; i < sheetCount; i++) {
+            Sheet sheet = workbook.getSheetAt(i);
+            //通过此处即可将excel中所有列放置在一页显示(打印),转换至pdf后也将在一页显示
+            XSSFPrintSetup printSetup = (XSSFPrintSetup) sheet.getPrintSetup();
+            printSetup.setFitHeight((short) 0);
+            sheet.setFitToPage(true);
+        }
+        workbook.write(new FileOutputStream(newFilePath));
+    }
+
+    /**
+     * excel超范围预处理
+     */
+    public InputStream refresh(InputStream oldInputSteam, String oldType) throws IOException {
+        Workbook workbook = null;
+        if (oldType.equals("xls")) {
+            workbook = new HSSFWorkbook(oldInputSteam);
+        } else if (oldType.equals("xlsx")) {
+            workbook = new XSSFWorkbook(oldInputSteam);
+        }
+        assert workbook != null;
+        int sheetCount = workbook.getNumberOfSheets();
+        for (int i = 0; i < sheetCount; i++) {
+            Sheet sheet = workbook.getSheetAt(i);
+            sheet.setHorizontallyCenter(true);//设置打印页面为水平居中
+            sheet.getPrintSetup().setFitHeight((short) 0);
+            sheet.setFitToPage(true);
+        }
+        String uuid = UUID.randomUUID().toString().replace("-", "");
+        String newFilePath = filePath + uuid + "changedFile.xlsx";
+        workbook.write(new FileOutputStream(newFilePath));
+        return  new FileInputStream(newFilePath);
+    }
+
+    public static void main(String[] args) throws Exception {
+    	convertNetFileToFile("https://file.test.com/test/20230320/88af234acc864bfb91de13c16b0469f8.docx", "docx", "C:\\Users\\cvec2022\\Desktop\\abc.pdf");
+    }
+}

+ 0 - 85
src/main/java/com/singularity/util/OpenOfficeUtil.java

@@ -1,85 +0,0 @@
-package com.singularity.util;
-
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.usermodel.Sheet;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.jodconverter.DocumentConverter;
-import org.jodconverter.document.DefaultDocumentFormatRegistry;
-import org.jodconverter.office.OfficeException;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.stereotype.Component;
-
-import javax.annotation.Resource;
-import java.io.*;
-import java.util.UUID;
-
-@Component
-public class OpenOfficeUtil {
-
-//    @Resource
-//    private DocumentConverter documentConverter;
-
-    @Value("${file.path}")
-    private String filePath;
-
-    public void wordToPDF(String srcPath, String destPath) throws OfficeException {
-        File file = new File(destPath);
-//        documentConverter.convert(new File(srcPath)).to(file).as(DefaultDocumentFormatRegistry.PDF).execute();
-    }
-
-    public void wordToPDF(InputStream src,OutputStream tar) throws OfficeException {
-//        documentConverter.convert(src).as(DefaultDocumentFormatRegistry.DOCX).to(tar).as(DefaultDocumentFormatRegistry.PDF).execute();
-    }
-
-    public void excelToPDF(InputStream src, OutputStream tar) throws OfficeException {
-//        documentConverter.convert(src).as(DefaultDocumentFormatRegistry.XLSX).to(tar).as(DefaultDocumentFormatRegistry.PDF).execute();
-    }
-
-    /**
-     * excel超范围预处理
-     */
-    public void refresh(String oldFilePath, String oldType, String newFilePath) throws IOException {
-        Workbook workbook = null;
-        if (oldType.equals("xls")) {
-            workbook = new HSSFWorkbook(new FileInputStream(oldFilePath));
-        } else if (oldType.equals("xlsx")) {
-            workbook = new XSSFWorkbook(new FileInputStream(oldFilePath));
-        }
-        assert workbook != null;
-        int sheetCount = workbook.getNumberOfSheets();
-        for (int i = 0; i < sheetCount; i++) {
-            Sheet sheet = workbook.getSheetAt(i);
-            //通过此处即可将excel中所有列放置在一页显示(打印),转换至pdf后也将在一页显示
-            XSSFPrintSetup printSetup = (XSSFPrintSetup) sheet.getPrintSetup();
-            printSetup.setFitHeight((short) 0);
-            sheet.setFitToPage(true);
-        }
-        workbook.write(new FileOutputStream(newFilePath));
-    }
-
-    /**
-     * excel超范围预处理
-     */
-    public InputStream refresh(InputStream oldInputSteam, String oldType) throws IOException {
-        Workbook workbook = null;
-        if (oldType.equals("xls")) {
-            workbook = new HSSFWorkbook(oldInputSteam);
-        } else if (oldType.equals("xlsx")) {
-            workbook = new XSSFWorkbook(oldInputSteam);
-        }
-        assert workbook != null;
-        int sheetCount = workbook.getNumberOfSheets();
-        for (int i = 0; i < sheetCount; i++) {
-            Sheet sheet = workbook.getSheetAt(i);
-            sheet.setHorizontallyCenter(true);//设置打印页面为水平居中
-            sheet.getPrintSetup().setFitHeight((short) 0);
-            sheet.setFitToPage(true);
-        }
-        String uuid = UUID.randomUUID().toString().replace("-", "");
-        String newFilePath = filePath + uuid + "changedFile.xlsx";
-        workbook.write(new FileOutputStream(newFilePath));
-        return  new FileInputStream(newFilePath);
-    }
-}

+ 0 - 7
src/main/resources/application-pro.yml

@@ -68,13 +68,6 @@ sa-token:
 file:
   path: /home/temporary_file
 
-#jodconverter:
-#  local:
-#    enabled: true
-#    office-home: /opt/openoffice4
-#    max-tasks-per-process: 10
-#    port-numbers: 8100
-
 detection:
   minio:
     endpoint: http://localhost:9000