mirror of
https://gitee.com/lab1024/smart-admin.git
synced 2025-09-17 19:06:39 +08:00
refactor(sa-base): 优化响应工具类 SmartResponseUtil
- 使用常量 MediaType.APPLICATION_JSON_VALUE 替代硬编码的 "application/json"- 使用 UTF_8 常量替代 "utf-8" 字符串 - 移除不必要的异常捕获 - 优化文件下载头信息设置方法
This commit is contained in:
parent
db5e0062a0
commit
ae753844c7
@ -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,21 +46,16 @@ public class SmartResponseUtil {
|
||||
}
|
||||
|
||||
public static void setDownloadFileHeader(HttpServletResponse response, String fileName, Long fileSize) {
|
||||
response.setCharacterEncoding("utf-8");
|
||||
try {
|
||||
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.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(fileName, StandardCharsets.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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user