From ae753844c76af9e48782a5501aa1c7ae24724999 Mon Sep 17 00:00:00 2001 From: CoderKK Date: Wed, 15 Jan 2025 10:47:55 +0800 Subject: [PATCH] =?UTF-8?q?refactor(sa-base):=20=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E5=93=8D=E5=BA=94=E5=B7=A5=E5=85=B7=E7=B1=BB=20SmartResponseUt?= =?UTF-8?q?il?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 使用常量 MediaType.APPLICATION_JSON_VALUE 替代硬编码的 "application/json"- 使用 UTF_8 常量替代 "utf-8" 字符串 - 移除不必要的异常捕获 - 优化文件下载头信息设置方法 --- .../base/common/util/SmartResponseUtil.java | 29 +++++++++---------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartResponseUtil.java b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartResponseUtil.java index 3ce4cc08..2594fa6e 100644 --- a/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartResponseUtil.java +++ b/smart-admin-api-java17-springboot3/sa-base/src/main/java/net/lab1024/sa/base/common/util/SmartResponseUtil.java @@ -9,8 +9,10 @@ import org.springframework.http.MediaType; import org.springframework.http.MediaTypeFactory; import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; + +import static cn.hutool.core.util.CharsetUtil.UTF_8; /** * 返回工具栏 @@ -27,8 +29,8 @@ public class SmartResponseUtil { public static void write(HttpServletResponse response, ResponseDTO responseDTO) { // 重置response - response.setContentType("application/json"); - response.setCharacterEncoding("utf-8"); + response.setContentType(MediaType.APPLICATION_JSON_VALUE); + response.setCharacterEncoding(UTF_8); try { response.getWriter().write(JSON.toJSONString(responseDTO)); @@ -44,20 +46,15 @@ public class SmartResponseUtil { } public static void setDownloadFileHeader(HttpServletResponse response, String fileName, Long fileSize) { - response.setCharacterEncoding("utf-8"); - try { - if (fileSize != null) { - response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileSize)); - } + response.setCharacterEncoding(UTF_8); + if (fileSize != null) { + response.setHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(fileSize)); + } - if (SmartStringUtil.isNotEmpty(fileName)) { - response.setHeader(HttpHeaders.CONTENT_TYPE, MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM) + ";charset=utf-8"); - response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20")); - response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION); - } - } catch (UnsupportedEncodingException e) { - log.error(e.getMessage(), e); - throw new RuntimeException(e); + if (SmartStringUtil.isNotEmpty(fileName)) { + response.setHeader(HttpHeaders.CONTENT_TYPE, MediaTypeFactory.getMediaType(fileName).orElse(MediaType.APPLICATION_OCTET_STREAM) + ";charset=utf-8"); + response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8).replaceAll("\\+", "%20")); + response.setHeader(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION); } }