修复: 文件名含有特殊字符时无法预览

dependabot/maven/server/com.thoughtworks.xstream-xstream-1.4.18
jerrykcode 2021-08-19 20:25:44 +08:00
parent 2395a489a3
commit 2177aed64f
2 changed files with 22 additions and 0 deletions

View File

@ -273,6 +273,7 @@ public class FileHandlerService {
attribute.setType(type); attribute.setType(type);
attribute.setName(fileName); attribute.setName(fileName);
attribute.setSuffix(suffix); attribute.setSuffix(suffix);
url = WebUtils.encodeUrlFileName(url);
attribute.setUrl(url); attribute.setUrl(url);
if (req != null) { if (req != null) {
String officePreviewType = req.getParameter("officePreviewType"); String officePreviewType = req.getParameter("officePreviewType");

View File

@ -2,8 +2,10 @@ package cn.keking.utils;
import io.mola.galimatias.GalimatiasParseException; import io.mola.galimatias.GalimatiasParseException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException; import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@ -97,4 +99,23 @@ public class WebUtils {
String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1); String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1);
return KkFileUtils.suffixFromFileName(fileName); return KkFileUtils.suffixFromFileName(fileName);
} }
/**
* urlUTF-8
*
* @param url url
* @return url
*/
public static String encodeUrlFileName(String url) {
String noQueryUrl = url.substring(0, url.contains("?") ? url.indexOf("?") : url.length());
int fileNameStartIndex = noQueryUrl.lastIndexOf('/') + 1;
int fileNameEndIndex = noQueryUrl.lastIndexOf('.');
String encodedFileName;
try {
encodedFileName = URLEncoder.encode(noQueryUrl.substring(fileNameStartIndex, fileNameEndIndex), "UTF-8");
} catch (UnsupportedEncodingException e) {
return null;
}
return url.substring(0, fileNameStartIndex) + encodedFileName + url.substring(fileNameEndIndex);
}
} }