diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java index ed0fe45e..fd90d950 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/http/servlet/RecordableServletHttpRequest.java @@ -23,6 +23,7 @@ import jakarta.servlet.http.HttpServletRequest; import org.springframework.web.util.UriUtils; import top.continew.starter.core.constant.StringConstants; import top.continew.starter.log.http.RecordableHttpRequest; +import top.continew.starter.web.util.RepeatReadRequestWrapper; import java.net.URI; import java.net.URISyntaxException; @@ -79,8 +80,11 @@ public final class RecordableServletHttpRequest implements RecordableHttpRequest @Override public String getBody() { - String body = JakartaServletUtil.getBody(request); - return JSONUtil.isTypeJSON(body) ? body : null; + if (request instanceof RepeatReadRequestWrapper wrapper && !wrapper.isMultipartContent(request)) { + String body = JakartaServletUtil.getBody(request); + return JSONUtil.isTypeJSON(body) ? body : null; + } + return null; } @Override diff --git a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java index 1ab72b26..010180c2 100644 --- a/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java +++ b/continew-starter-log/continew-starter-log-core/src/main/java/top/continew/starter/log/util/AccessLogUtils.java @@ -48,7 +48,13 @@ public class AccessLogUtils { } // 参数为空返回空 - Map params = request.getParam(); + Map params; + try { + params = request.getParam(); + } catch (Exception e) { + return null; + } + if (ObjectUtil.isEmpty(params) || params.isEmpty()) { return null; } diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadRequestWrapper.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadRequestWrapper.java index e9c36f8c..5bb3a1d7 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadRequestWrapper.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadRequestWrapper.java @@ -87,18 +87,18 @@ public class RepeatReadRequestWrapper extends HttpServletRequestWrapper { public BufferedReader getReader() throws IOException { // 如果是文件上传,直接返回原始Reader if (isMultipartContent(originalRequest)) { - return originalRequest.getReader(); + new BufferedReader(new InputStreamReader(originalRequest.getInputStream(), StandardCharsets.UTF_8)); } - return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(cachedBody), StandardCharsets.UTF_8)); + return new BufferedReader(new InputStreamReader(getInputStream())); } /** * 检查是否为文件上传请求 - * + * * @param request 请求对象 * @return 是否为文件上传请求 */ - private boolean isMultipartContent(HttpServletRequest request) { + public boolean isMultipartContent(HttpServletRequest request) { return request.getContentType() != null && request.getContentType().toLowerCase().startsWith("multipart/"); } } diff --git a/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadResponseWrapper.java b/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadResponseWrapper.java index aa3080cb..c64e6bd3 100644 --- a/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadResponseWrapper.java +++ b/continew-starter-web/src/main/java/top/continew/starter/web/util/RepeatReadResponseWrapper.java @@ -20,6 +20,7 @@ import jakarta.servlet.ServletOutputStream; import jakarta.servlet.WriteListener; import jakarta.servlet.http.HttpServletResponse; import jakarta.servlet.http.HttpServletResponseWrapper; +import org.springframework.http.MediaType; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -54,7 +55,7 @@ public class RepeatReadResponseWrapper extends HttpServletResponseWrapper { // 根据 Content-Type 判断是否为流式响应 if (type != null) { String lowerType = type.toLowerCase(); - isStreamingResponse = lowerType.contains("text/event-stream"); + isStreamingResponse = lowerType.contains(MediaType.TEXT_EVENT_STREAM_VALUE); } } @@ -62,7 +63,7 @@ public class RepeatReadResponseWrapper extends HttpServletResponseWrapper { String contentType = getContentType(); if (contentType != null) { String lowerType = contentType.toLowerCase(); - isStreamingResponse = lowerType.contains("text/event-stream"); + isStreamingResponse = lowerType.contains(MediaType.TEXT_EVENT_STREAM_VALUE); } }