From 9225626f76d0c909c0b309605c5d5f9d1093ec06 Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Thu, 31 Aug 2023 18:53:30 +0800 Subject: [PATCH] Fix the stream closed error --- .../web/servlet/ContentCachingRequest.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java b/commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java index 9e48a53..8a1da89 100644 --- a/commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java +++ b/commons/web-common/src/main/java/com/schbrain/common/web/servlet/ContentCachingRequest.java @@ -14,18 +14,15 @@ import java.io.IOException; @Slf4j public class ContentCachingRequest extends HttpServletRequestWrapper { - private WrappedByteArrayInputStream inputStream; + private final WrappedByteArrayInputStream inputStream; public ContentCachingRequest(HttpServletRequest request) { super(request); + this.inputStream = initWrappedInputStream(request); } @Override - public WrappedByteArrayInputStream getInputStream() throws IOException { - if (inputStream == null) { - byte[] bytes = StreamUtils.copyToByteArray(super.getInputStream()); - this.inputStream = new WrappedByteArrayInputStream(bytes); - } + public WrappedByteArrayInputStream getInputStream() { return inputStream; } @@ -40,8 +37,13 @@ public class ContentCachingRequest extends HttpServletRequestWrapper { * Return the cached request content as a String */ public String getContentAsString(String charset) { + return inputStream.getContentAsString(charset); + } + + private WrappedByteArrayInputStream initWrappedInputStream(HttpServletRequest request) { try { - return getInputStream().getContentAsString(charset); + byte[] bytes = StreamUtils.copyToByteArray(request.getInputStream()); + return new WrappedByteArrayInputStream(bytes); } catch (IOException e) { throw new IllegalStateException(e.getMessage(), e); } -- GitLab