From 2177aed64fca4bad1656267b9802872cfe5fa19a Mon Sep 17 00:00:00 2001 From: jerrykcode Date: Thu, 19 Aug 2021 20:25:44 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E5=A4=8D:=20=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=90=8D=E5=90=AB=E6=9C=89=E7=89=B9=E6=AE=8A=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E6=97=B6=E6=97=A0=E6=B3=95=E9=A2=84=E8=A7=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/keking/service/FileHandlerService.java | 1 + .../main/java/cn/keking/utils/WebUtils.java | 21 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/server/src/main/java/cn/keking/service/FileHandlerService.java b/server/src/main/java/cn/keking/service/FileHandlerService.java index d8ba3122..b0901301 100644 --- a/server/src/main/java/cn/keking/service/FileHandlerService.java +++ b/server/src/main/java/cn/keking/service/FileHandlerService.java @@ -273,6 +273,7 @@ public class FileHandlerService { attribute.setType(type); attribute.setName(fileName); attribute.setSuffix(suffix); + url = WebUtils.encodeUrlFileName(url); attribute.setUrl(url); if (req != null) { String officePreviewType = req.getParameter("officePreviewType"); diff --git a/server/src/main/java/cn/keking/utils/WebUtils.java b/server/src/main/java/cn/keking/utils/WebUtils.java index ec67f0b9..c978e59c 100644 --- a/server/src/main/java/cn/keking/utils/WebUtils.java +++ b/server/src/main/java/cn/keking/utils/WebUtils.java @@ -2,8 +2,10 @@ package cn.keking.utils; import io.mola.galimatias.GalimatiasParseException; +import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; +import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; @@ -97,4 +99,23 @@ public class WebUtils { String fileName = nonPramStr.substring(nonPramStr.lastIndexOf("/") + 1); return KkFileUtils.suffixFromFileName(fileName); } + + /** + * 对url中的文件名进行UTF-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); + } } From f2d5f4a86c6b5a5408bda465688de42e7696c840 Mon Sep 17 00:00:00 2001 From: jerrykcode Date: Fri, 20 Aug 2021 11:38:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E4=B8=BAWebUtils.encodeUrlFileName?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95=E7=94=A8?= =?UTF-8?q?=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/keking/utils/WebUtilsTests.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 server/src/test/java/cn/keking/utils/WebUtilsTests.java diff --git a/server/src/test/java/cn/keking/utils/WebUtilsTests.java b/server/src/test/java/cn/keking/utils/WebUtilsTests.java new file mode 100644 index 00000000..59107cd1 --- /dev/null +++ b/server/src/test/java/cn/keking/utils/WebUtilsTests.java @@ -0,0 +1,24 @@ +package cn.keking.utils; + +import org.junit.jupiter.api.Test; + +public class WebUtilsTests { + + @Test + void encodeUrlFileNameTest() { + // 测试对URL中的文件名部分进行UTF-8编码 + String in = "https://file.keking.cn/demo/hello#0.txt"; + String out = "https://file.keking.cn/demo/hello%230.txt"; + assert WebUtils.encodeUrlFileName(in).equals(out); + } + + @Test + void encodeUrlFileNameTestWithParams() { + // 测试对URL中的文件名部分进行UTF-8编码 + // URL带参数 + // 文件名"#hello&world"中的"&"应该被编码成为"%26",而?后的参数列表中的"&"不会被编码 + String in = "https://file.keking.cn/demo/#hello&world.txt?param0=0¶m1=1"; + String out = "https://file.keking.cn/demo/%23hello%26world.txt?param0=0¶m1=1"; + assert WebUtils.encodeUrlFileName(in).equals(out); + } +}