Przeglądaj źródła

修复特殊符号的文件无法删除 (#427)

* 修复特殊符号的文件无法删除

* cad 优化

Co-authored-by: gaoxiongzaq <admin@cxcp.com>
gaoxingzaq 2 lat temu
rodzic
commit
c0cf4fcc39

+ 19 - 11
server/src/main/java/cn/keking/service/FileHandlerService.java

@@ -6,7 +6,10 @@ import cn.keking.model.FileType;
 import cn.keking.service.cache.CacheService;
 import cn.keking.utils.KkFileUtils;
 import cn.keking.utils.WebUtils;
+import com.aspose.cad.CodePages;
 import com.aspose.cad.Color;
+import com.aspose.cad.Image;
+import com.aspose.cad.LoadOptions;
 import com.aspose.cad.fileformats.cad.CadDrawTypeMode;
 import com.aspose.cad.imageoptions.CadRasterizationOptions;
 import com.aspose.cad.imageoptions.PdfOptions;
@@ -227,29 +230,34 @@ public class FileHandlerService {
      * @return 转换是否成功
      */
     public boolean cadToPdf(String inputFilePath, String outputFilePath)  {
-        com.aspose.cad.Image cadImage = com.aspose.cad.Image.load(inputFilePath);
+        File outputFile = new File(outputFilePath);
+        LoadOptions opts = new LoadOptions();
+        opts.setSpecifiedEncoding(CodePages.SimpChinese);
+        com.aspose.cad.Image cadImage = Image.load(inputFilePath, opts);
         CadRasterizationOptions cadRasterizationOptions = new CadRasterizationOptions();
-        cadRasterizationOptions.setLayouts(new String[]{"Model"});
-        cadRasterizationOptions.setNoScaling(true);
         cadRasterizationOptions.setBackgroundColor(Color.getWhite());
-        cadRasterizationOptions.setPageWidth(cadImage.getWidth());
-        cadRasterizationOptions.setPageHeight(cadImage.getHeight());
-        cadRasterizationOptions.setPdfProductLocation("center");
+        cadRasterizationOptions.setPageWidth(1400);
+        cadRasterizationOptions.setPageHeight(650);
         cadRasterizationOptions.setAutomaticLayoutsScaling(true);
-        cadRasterizationOptions.setDrawType(CadDrawTypeMode.UseObjectColor);
+        cadRasterizationOptions.setNoScaling (false);
+        cadRasterizationOptions.setDrawType(1);
         PdfOptions pdfOptions = new PdfOptions();
         pdfOptions.setVectorRasterizationOptions(cadRasterizationOptions);
-        File outputFile = new File(outputFilePath);
         OutputStream stream;
         try {
             stream = new FileOutputStream(outputFile);
             cadImage.save(stream, pdfOptions);
+            stream.close();
             cadImage.close();
             return true;
-        } catch (FileNotFoundException e) {
+        } catch (IOException e) {
             logger.error("PDFFileNotFoundException,inputFilePath:{}", inputFilePath, e);
-            return false;
+        }finally{
+            if(cadImage != null){   //关闭
+                cadImage.close();
+            }
         }
+        return false;
     }
 
     /**
@@ -277,7 +285,7 @@ public class FileHandlerService {
             attribute.setSkipDownLoad(true);
         }
         url = WebUtils.encodeUrlFileName(url);
-        fileName =  KkFileUtils.htmlEscape(fileName);  //文件名处理
+       fileName =  KkFileUtils.htmlEscape(fileName);  //文件名处理
         attribute.setType(type);
         attribute.setName(fileName);
         attribute.setSuffix(suffix);

+ 7 - 12
server/src/main/java/cn/keking/web/controller/FileController.java

@@ -3,6 +3,7 @@ package cn.keking.web.controller;
 import cn.keking.config.ConfigConstants;
 import cn.keking.model.ReturnResponse;
 import cn.keking.utils.KkFileUtils;
+import cn.keking.utils.WebUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.util.StreamUtils;
@@ -17,17 +18,10 @@ import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Paths;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
+import java.util.*;
 
 /**
  * @author yudian-it
@@ -41,7 +35,7 @@ public class FileController {
     private final String fileDir = ConfigConstants.getFileDir();
     private final String demoDir = "demo";
     private final String demoPath = demoDir + File.separator;
-
+    public static final String BASE64_DECODE_ERROR_MSG = "Base64解码失败,请检查你的 %s 是否采用 Base64 + urlEncode 双重编码了!";
     @PostMapping("/fileUpload")
     public ReturnResponse<Object> fileUpload(@RequestParam("file") MultipartFile file) {
         if (ConfigConstants.getFileUploadDisable()) {
@@ -99,9 +93,10 @@ public class FileController {
             return ReturnResponse.failure("文件名为空,删除失败!");
         }
         try {
-            fileName = URLDecoder.decode(fileName, StandardCharsets.UTF_8.name());
-        } catch (UnsupportedEncodingException e) {
-            e.printStackTrace();
+            fileName = WebUtils.decodeUrl(fileName);
+        } catch (Exception ex) {
+            String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url");
+            return ReturnResponse.failure(errorMsg+"删除失败!");
         }
         if (fileName.contains("/")) {
             fileName = fileName.substring(fileName.lastIndexOf("/") + 1);

+ 2 - 3
server/src/main/resources/web/index.ftl

@@ -148,7 +148,7 @@
 <script>
     function deleteFile(fileName) {
         $.ajax({
-            url: '${baseUrl}deleteFile?fileName=' + encodeURIComponent(fileName),
+            url: '${baseUrl}deleteFile?fileName=' + fileName,
             success: function (data) {
                 // 删除完成,刷新table
                 if (1 === data.code) {
@@ -209,9 +209,8 @@
             // 每个data添加一列用来操作
             $(data).each(function (index, item) {
                 item.action = "<a class='btn btn-success' target='_blank' href='${baseUrl}onlinePreview?url=" + encodeURIComponent(Base64.encode('${baseUrl}' + item.fileName)) + "'>预览</a>" +
-                    "<a class='btn btn-danger' style='margin-left:10px;' href='javascript:void(0);' onclick='deleteFile(\"" + item.fileName + "\")'>删除</a>";
+                "<a class='btn btn-danger' style='margin-left:10px;' href='javascript:void(0);' onclick='deleteFile(\"" +  encodeURIComponent(Base64.encode('${baseUrl}' + item.fileName)) + "\")'>删除</a>";
             });
-
             return data;
         }).on('post-body.bs.table', function (e, data) {
             return data;