Browse Source

!275 修复 url特殊符号无法下载或者输出问题 修复流接入方法 拼接字符导导致下载 或者跨域错误问题
Merge pull request !275 from 高雄/master

kailing 1 year ago
parent
commit
312c31a426

+ 4 - 7
server/src/main/java/cn/keking/service/FileHandlerService.java

@@ -437,11 +437,7 @@ public class FileHandlerService implements InitializingBean {
             type = FileType.typeFromFileName(fullFileName);
             suffix = KkFileUtils.suffixFromFileName(fullFileName);
             // 移除fullfilename参数
-            if (url.indexOf("fullfilename=" + fullFileName + "&") > 0) {
-                url = url.replace("fullfilename=" + fullFileName + "&", "");
-            } else {
-                url = url.replace("fullfilename=" + fullFileName, "");
-            }
+            url = WebUtils.clearFullfilenameParam(url);
         } else {
             originFileName = WebUtils.getFileNameFromURL(url);
             type = FileType.typeFromUrl(url);
@@ -460,13 +456,14 @@ public class FileHandlerService implements InitializingBean {
                 e.printStackTrace();
             }
         }
-        url = WebUtils.encodeUrlFileName(url);
         if (UrlEncoderUtils.hasUrlEncoded(originFileName)) {  //判断文件名是否转义
             try {
-                originFileName = URLDecoder.decode(originFileName, uriEncoding).replaceAll("\\+", "%20");
+                originFileName = URLDecoder.decode(originFileName, uriEncoding);  //转义的文件名 解下出原始文件名
             } catch (UnsupportedEncodingException e) {
                 e.printStackTrace();
             }
+        }else {
+            url = WebUtils.encodeUrlFileName(url); //对未转义的url进行转义
         }
         originFileName = KkFileUtils.htmlEscape(originFileName);  //文件名处理
         boolean isHtmlView = suffix.equalsIgnoreCase("xls") || suffix.equalsIgnoreCase("xlsx") || suffix.equalsIgnoreCase("csv") || suffix.equalsIgnoreCase("xlsm") || suffix.equalsIgnoreCase("xlt") || suffix.equalsIgnoreCase("xltm") || suffix.equalsIgnoreCase("et") || suffix.equalsIgnoreCase("ett") || suffix.equalsIgnoreCase("xlam");

+ 2 - 1
server/src/main/java/cn/keking/service/impl/CadFilePreviewImpl.java

@@ -7,10 +7,10 @@ import cn.keking.service.FileHandlerService;
 import cn.keking.service.FilePreview;
 import cn.keking.utils.DownloadUtils;
 import cn.keking.utils.KkFileUtils;
+import cn.keking.utils.WebUtils;
 import cn.keking.web.filter.BaseUrlFilter;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.Model;
-import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
 import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
@@ -70,6 +70,7 @@ public class CadFilePreviewImpl implements FilePreview {
                 }
             }
         }
+        cacheName=  WebUtils.encodeFileName(cacheName);
         if ("tif".equalsIgnoreCase(cadPreviewType)) {
             model.addAttribute("currentUrl", cacheName);
             return TIFF_FILE_PREVIEW_PAGE;

+ 2 - 2
server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java

@@ -9,13 +9,13 @@ import cn.keking.service.OfficeToPdfService;
 import cn.keking.utils.DownloadUtils;
 import cn.keking.utils.KkFileUtils;
 import cn.keking.utils.OfficeUtils;
+import cn.keking.utils.WebUtils;
 import cn.keking.web.filter.BaseUrlFilter;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.poi.EncryptedDocumentException;
 import org.jodconverter.core.office.OfficeException;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.Model;
-import org.springframework.util.ObjectUtils;
 import org.springframework.util.StringUtils;
 
 import java.io.IOException;
@@ -111,7 +111,7 @@ public class OfficeFilePreviewImpl implements FilePreview {
         if (!isHtmlView && baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
             return getPreviewType(model, fileAttribute, officePreviewType, cacheName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE, otherFilePreview);
         }
-        model.addAttribute("pdfUrl", cacheName);
+        model.addAttribute("pdfUrl", WebUtils.encodeFileName(cacheName));  //输出转义文件名 方便url识别
         return isHtmlView ? EXEL_FILE_PREVIEW_PAGE : PDF_FILE_PREVIEW_PAGE;
     }
 

+ 3 - 3
server/src/main/java/cn/keking/service/impl/PdfFilePreviewImpl.java

@@ -3,16 +3,16 @@ package cn.keking.service.impl;
 import cn.keking.config.ConfigConstants;
 import cn.keking.model.FileAttribute;
 import cn.keking.model.ReturnResponse;
+import cn.keking.service.FileHandlerService;
 import cn.keking.service.FilePreview;
 import cn.keking.utils.DownloadUtils;
-import cn.keking.service.FileHandlerService;
+import cn.keking.utils.WebUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
 import org.apache.poi.EncryptedDocumentException;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.Model;
 
 import java.io.IOException;
-import java.net.URLEncoder;
 import java.util.List;
 
 /**
@@ -88,7 +88,7 @@ public class PdfFilePreviewImpl implements FilePreview {
                         fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
                     }
                 } else {
-                    model.addAttribute("pdfUrl", pdfName);
+                    model.addAttribute("pdfUrl", WebUtils.encodeFileName(pdfName));
                 }
             } else {
                 model.addAttribute("pdfUrl", url);

+ 4 - 5
server/src/main/java/cn/keking/service/impl/TiffFilePreviewImpl.java

@@ -8,11 +8,10 @@ import cn.keking.service.FilePreview;
 import cn.keking.utils.ConvertPicUtil;
 import cn.keking.utils.DownloadUtils;
 import cn.keking.utils.KkFileUtils;
+import cn.keking.utils.WebUtils;
 import org.springframework.stereotype.Service;
 import org.springframework.ui.Model;
-import org.springframework.util.ObjectUtils;
 
-import java.io.IOException;
 import java.util.List;
 
 /**
@@ -64,7 +63,7 @@ public class TiffFilePreviewImpl implements FilePreview {
                         // 加入缓存
                         fileHandlerService.addConvertedFile(cacheName, fileHandlerService.getRelativePath(outFilePath));
                     }
-                    model.addAttribute("pdfUrl", cacheName);
+                    model.addAttribute("pdfUrl", WebUtils.encodeFileName(cacheName));
                     return PDF_FILE_PREVIEW_PAGE;
                 }else {
                     // 将tif转换为jpg,返回转换后的文件路径、文件名的list
@@ -95,7 +94,7 @@ public class TiffFilePreviewImpl implements FilePreview {
                 }
             }
             if ("pdf".equalsIgnoreCase(tifPreviewType)) {
-                model.addAttribute("pdfUrl", fileHandlerService.listConvertedFiles().get(cacheName));
+                model.addAttribute("pdfUrl", WebUtils.encodeFileName(cacheName));
                 return PDF_FILE_PREVIEW_PAGE;
             }
             else if ("jpg".equalsIgnoreCase(tifPreviewType)) {
@@ -117,7 +116,7 @@ public class TiffFilePreviewImpl implements FilePreview {
                     fileHandlerService.addConvertedFile(fileName, fileHandlerService.getRelativePath(outFilePath));
                 }
             } else {
-                model.addAttribute("currentUrl",  fileName);
+                model.addAttribute("currentUrl",  WebUtils.encodeFileName(fileName));
             }
             return TIFF_FILE_PREVIEW_PAGE;
         }

+ 1 - 2
server/src/main/java/cn/keking/utils/DownloadUtils.java

@@ -105,8 +105,7 @@ public class DownloadUtils {
                         }
                     };
                     try {
-                        URI uri = URI.create(urlStr);
-                        restTemplate.execute(uri, HttpMethod.GET, requestCallback, fileResponse -> {
+                        restTemplate.execute(url.toURI(), HttpMethod.GET, requestCallback, fileResponse -> {
                             FileUtils.copyToFile(fileResponse.getBody(), realFile);
                             return null;
                         });

+ 47 - 0
server/src/main/java/cn/keking/utils/WebUtils.java

@@ -5,6 +5,7 @@ import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.util.Base64Utils;
+import org.springframework.util.ObjectUtils;
 import org.springframework.web.multipart.MultipartFile;
 import org.springframework.web.util.HtmlUtils;
 
@@ -40,6 +41,52 @@ public class WebUtils {
         return io.mola.galimatias.URL.parse(urlStr).toJavaURL();
     }
 
+
+    /**
+     * 对文件名进行编码
+     *
+     */
+    public static String encodeFileName(String name) {
+        try {
+            name = URLEncoder.encode(name, "UTF-8").replaceAll("\\+", "%20");
+        } catch (UnsupportedEncodingException e) {
+            return null;
+        }
+        return name;
+    }
+
+
+    /**
+     * 去除fullfilename参数
+     *
+     * @param urlStr
+     * @return
+     */
+    public static String clearFullfilenameParam(String urlStr) {
+        // 去除特定参数字段
+        Pattern pattern = Pattern.compile("(&fullfilename=[^&]*)");
+        Matcher matcher = pattern.matcher(urlStr);
+        return matcher.replaceAll("");
+    }
+
+    /**
+     * 对URL进行编码
+     */
+    public static String  urlEncoderencode(String urlStr) {
+
+        String fullFileName = getUrlParameterReg(urlStr, "fullfilename");  //获取文件名
+        if (!ObjectUtils.isEmpty(fullFileName)) {  //判断是否启用了 流接入方法
+            urlStr = clearFullfilenameParam(urlStr);   //去掉流接入 拼接命令
+        }
+        try {
+            urlStr = URLEncoder.encode(urlStr, "UTF-8").replaceAll("\\+", "%20");
+            urlStr = urlStr.replaceAll("%3A", ":").replaceAll("%2F", "/").replaceAll("%3F", "?").replaceAll("%26", "&");
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+        return urlStr;
+    }
+
     /**
      * 获取url中的参数
      *

+ 2 - 2
server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java

@@ -64,11 +64,11 @@ public class OnlinePreviewController {
             String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url");
             return otherFilePreview.notSupportedFile(model, errorMsg);
         }
-        FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req);
+        FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req);  //这里不在进行URL 处理了
         model.addAttribute("file", fileAttribute);
         FilePreview filePreview = previewFactory.get(fileAttribute);
         logger.info("预览文件url:{},previewType:{}", fileUrl, fileAttribute.getType());
-        return filePreview.filePreviewHandle(fileUrl, model, fileAttribute);
+        return filePreview.filePreviewHandle(WebUtils.urlEncoderencode(fileUrl), model, fileAttribute);  //统一在这里处理 url
     }
 
     @GetMapping( "/picturesPreview")