瀏覽代碼

PDF BUG修复

wangliang 3 年之前
父節點
當前提交
5fefeaad9b

+ 9 - 6
distributed-print-business/src/main/java/com/qmth/distributed/print/business/service/impl/PrintCommonServiceServiceImpl.java

@@ -43,10 +43,7 @@ import org.springframework.util.FileCopyUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import javax.annotation.Resource;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.IOException;
+import java.io.*;
 import java.math.BigDecimal;
 import java.time.LocalDateTime;
 import java.util.*;
@@ -146,6 +143,7 @@ public class PrintCommonServiceServiceImpl implements PrintCommonService {
     @Override
     @Transactional
     public BasicAttachment saveAttachmentPdf(ClassifyEnum classifyEnum, ExamDetail examDetail, BasicAttachment basicAttachment, List<PdfDto> pdfList, Integer printCount, Integer sequence) throws IOException, DocumentException {
+        InputStream inputStream = null;
         try {
             boolean oss = dictionaryConfig.sysDomain().isOss();
             JSONObject jsonObject = JSONObject.parseObject(basicAttachment.getPath());
@@ -187,12 +185,13 @@ public class PrintCommonServiceServiceImpl implements PrintCommonService {
             HtmlToPdfUtil.convert(htmlFile.getPath(), destUrl, PageSizeEnum.A4);
 //            File pdfFile = asposePdfUtil.documentToPdf(htmlFile.getPath(), destUrl, PaperSize.A4);
             String htmlFileMd5 = DigestUtils.md5Hex(new FileInputStream(htmlFile));
-            String pdfFileMd5 = DigestUtils.md5Hex(new FileInputStream(pdfFile));
+            inputStream = new FileInputStream(pdfFile);
+            String pdfFileMd5 = DigestUtils.md5Hex(inputStream);
             PdfDto pdfDto = null;
             if (oss) {//上传至oss
                 pdfDto = PdfUtil.addPdfPage(pdfFile);
 //                ossUtil.ossUpload(pdfDirNameStr, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(pdfFileMd5)));
-                fileStoreUtil.ossUpload(pdfDirNameStr, pdfFile, BinaryUtil.toBase64String(HexUtils.decodeHex(pdfFileMd5)), fileStoreUtil.getUploadEnumByPath(pdfDirNameStr).getFssType());
+                fileStoreUtil.ossUpload(pdfDirNameStr, inputStream, BinaryUtil.toBase64String(HexUtils.decodeHex(pdfFileMd5)), fileStoreUtil.getUploadEnumByPath(pdfDirNameStr).getFssType());
 //                htmlFile.delete();
             }
             JSONObject attachmentPath = JSONObject.parseObject(examDetail.getAttachmentPath());
@@ -224,6 +223,10 @@ public class PrintCommonServiceServiceImpl implements PrintCommonService {
             } else {
                 ResultUtil.error(e.getMessage());
             }
+        } finally {
+            if (Objects.nonNull(inputStream)) {
+                inputStream.close();
+            }
         }
         return basicAttachment;
     }

+ 12 - 10
teachcloud-common/src/main/java/com/qmth/teachcloud/common/util/FileStoreUtil.java

@@ -35,9 +35,9 @@ public class FileStoreUtil {
      * @param inputStream 流
      * @param type        fileStore类型
      */
-    public void ossUpload(String dirName, InputStream inputStream,String md5, String type) throws Exception {
+    public void ossUpload(String dirName, InputStream inputStream, String md5, String type) throws Exception {
         log.info("ossUpload is come in");
-        fileService.getFileStore(type).write(dirName, inputStream,md5);
+        fileService.getFileStore(type).write(dirName, inputStream, md5);
         log.info("dirName:{}", dirName);
     }
 
@@ -48,7 +48,7 @@ public class FileStoreUtil {
      * @param file    文件
      * @param type    fileStore类型
      */
-    public void ossUpload(String dirName, File file,String md5, String type) throws Exception {
+    public void ossUpload(String dirName, File file, String md5, String type) throws Exception {
         log.info("ossUpload is come in");
         fileService.getFileStore(type).write(dirName, new FileInputStream(file), md5);
         log.info("dirName:{}", dirName);
@@ -108,17 +108,18 @@ public class FileStoreUtil {
 
     /**
      * 获取文件访问url
+     *
      * @param objectPath 文件路径
-     * @param type 文件上传的类型
+     * @param type       文件上传的类型
      * @return
      */
     public String getPrivateUrl(String objectPath, String type) {
         String server;
-        if ("public".equals(type)){
+        if ("public".equals(type)) {
             server = dictionaryConfig.fssPublicDomain().getServer();
-        }else if ("private".equals(type)){
+        } else if ("private".equals(type)) {
             server = dictionaryConfig.fssPrivateDomain().getServer();
-        }else {
+        } else {
             throw ExceptionResultEnum.ERROR.exception("文件存储store类型不存在");
         }
 
@@ -127,12 +128,13 @@ public class FileStoreUtil {
 
     /**
      * 根据数据库文件路径判断文件上传类型
+     *
      * @param path 路径
      * @return 类型
      */
-    public UploadFileEnum getUploadEnumByPath(String path){
-        path = path.replaceAll("\\\\","/");
-        String target = path.substring(0,path.indexOf('/'));
+    public UploadFileEnum getUploadEnumByPath(String path) {
+        path = path.replaceAll("\\\\", "/");
+        String target = path.substring(0, path.indexOf('/'));
         return UploadFileEnum.valueOf(target.toUpperCase());
     }