From 3009c07ed64702d3418a1f73656c9f752ecb36dc Mon Sep 17 00:00:00 2001 From: dongzeliang <2746733890@qq.com> Date: Thu, 27 Feb 2025 16:56:57 +0800 Subject: [PATCH] =?UTF-8?q?fix():=E6=A0=BC=E5=BC=8F=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/core/utils/file/FileUtils.java | 76 +++++++++++++++---- .../common/core/utils/html/EscapeUtil.java | 11 +-- 2 files changed, 61 insertions(+), 26 deletions(-) diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileUtils.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileUtils.java index 23dcead..73c7466 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileUtils.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/file/FileUtils.java @@ -29,8 +29,59 @@ public class FileUtils { */ public static final char BACKSLASH = '\\'; + /** + * 文件名称正则 + */ public final static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+"; + /** + * 上级目录 + */ + private final static String LAST_PATH = ".."; + + /** + * 请求对象 + */ + private final static String AGENT_KEY = "USER-AGENT"; + /** + * 微软IE + */ + private final static String MSIE = "MSIE"; + + /** + * 火狐浏览器 + */ + private final static String FIREFOX = "Firefox"; + + /** + * 谷歌浏览器 + */ + private final static String CHROME = "Chrome"; + + /** + * 文件模板描述 + */ + private final static String CONTENT_DISPOSITION_VALUE_TEMPLATE = "attachment; filename={};filename*=utf-8''{}"; + /** + * 描述 + */ + private final static String CONTENT_DISPOSITION = "Content-disposition"; + + /** + * 编码 + */ + private final static String CONTENT_ENCODING = "Content-encoding"; + + /** + * 百分号编码 + */ + private final static String PERCENT_SIGN_ENCODE = "\\+"; + + /** + * 百分号解码 + */ + private final static String PERCENT_SIGN_DECODE = "%20"; + /** * 输出指定文件的byte数组 * @@ -105,7 +156,7 @@ public class FileUtils { */ public static boolean checkAllowDownload (String resource) { // 禁止目录上跳级别 - if (StringUtils.contains(resource, "..")) { + if (StringUtils.contains(resource, LAST_PATH)) { return false; } // 判断是否在允许下载的文件规则内 @@ -121,16 +172,16 @@ public class FileUtils { * @return 编码后的文件名 */ public static String setFileDownloadHeader (HttpServletRequest request, String fileName) { - final String agent = request.getHeader("USER-AGENT"); + final String agent = request.getHeader(AGENT_KEY); String filename = fileName; - if (agent.contains("MSIE")) { + if (agent.contains(MSIE)) { // IE浏览器 filename = URLEncoder.encode(filename, StandardCharsets.UTF_8); filename = filename.replace(ADDITION_STR, EMPTY_STR); - } else if (agent.contains("Firefox")) { + } else if (agent.contains(FIREFOX)) { // 火狐浏览器 filename = new String(fileName.getBytes(), StandardCharsets.ISO_8859_1); - } else if (agent.contains("Chrome")) { + } else if (agent.contains(CHROME)) { // google浏览器 filename = URLEncoder.encode(filename, StandardCharsets.UTF_8); } else { @@ -194,16 +245,9 @@ public class FileUtils { */ public static void setAttachmentResponseHeader (HttpServletResponse response, String realFileName) throws UnsupportedEncodingException { String percentEncodedFileName = percentEncode(realFileName); - - String contentDispositionValue = "attachment; filename=" + - percentEncodedFileName + - ";" + - "filename*=" + - "utf-8''" + - percentEncodedFileName; - - response.setHeader("Content-disposition", contentDispositionValue); - response.setHeader("download-filename", percentEncodedFileName); + String contentDispositionValue = StringUtils.format(CONTENT_DISPOSITION_VALUE_TEMPLATE, percentEncodedFileName, percentEncodedFileName); + response.setHeader(CONTENT_DISPOSITION, contentDispositionValue); + response.setHeader(CONTENT_ENCODING, percentEncodedFileName); } /** @@ -215,6 +259,6 @@ public class FileUtils { */ public static String percentEncode (String s) { String encode = URLEncoder.encode(s, StandardCharsets.UTF_8); - return encode.replaceAll("\\+", "%20"); + return encode.replaceAll(PERCENT_SIGN_ENCODE, PERCENT_SIGN_DECODE); } } diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/html/EscapeUtil.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/html/EscapeUtil.java index cd6ec5f..a97152e 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/html/EscapeUtil.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/html/EscapeUtil.java @@ -17,7 +17,6 @@ public class EscapeUtil { TEXT[i] = new char[]{(char) i}; } - // special HTML characters // 单引号 TEXT['\''] = "'".toCharArray(); // 双引号 @@ -129,19 +128,11 @@ public class EscapeUtil { tmp.append(content.substring(lastPos)); lastPos = content.length(); } else { - tmp.append(content.substring(lastPos, pos)); + tmp.append(content, lastPos, pos); lastPos = pos; } } } return tmp.toString(); } - - public static void main (String[] args) { - String html = ""; - String escape = EscapeUtil.escape(html); - System.out.println("clean: " + EscapeUtil.clean(html)); - System.out.println("escape: " + escape); - System.out.println("unescape: " + EscapeUtil.unescape(escape)); - } }