Commit 9225626f authored by liaozan's avatar liaozan 🏀

Fix the stream closed error

parent 1bd7ef80
...@@ -14,18 +14,15 @@ import java.io.IOException; ...@@ -14,18 +14,15 @@ import java.io.IOException;
@Slf4j @Slf4j
public class ContentCachingRequest extends HttpServletRequestWrapper { public class ContentCachingRequest extends HttpServletRequestWrapper {
private WrappedByteArrayInputStream inputStream; private final WrappedByteArrayInputStream inputStream;
public ContentCachingRequest(HttpServletRequest request) { public ContentCachingRequest(HttpServletRequest request) {
super(request); super(request);
this.inputStream = initWrappedInputStream(request);
} }
@Override @Override
public WrappedByteArrayInputStream getInputStream() throws IOException { public WrappedByteArrayInputStream getInputStream() {
if (inputStream == null) {
byte[] bytes = StreamUtils.copyToByteArray(super.getInputStream());
this.inputStream = new WrappedByteArrayInputStream(bytes);
}
return inputStream; return inputStream;
} }
...@@ -40,8 +37,13 @@ public class ContentCachingRequest extends HttpServletRequestWrapper { ...@@ -40,8 +37,13 @@ public class ContentCachingRequest extends HttpServletRequestWrapper {
* Return the cached request content as a String * Return the cached request content as a String
*/ */
public String getContentAsString(String charset) { public String getContentAsString(String charset) {
return inputStream.getContentAsString(charset);
}
private WrappedByteArrayInputStream initWrappedInputStream(HttpServletRequest request) {
try { try {
return getInputStream().getContentAsString(charset); byte[] bytes = StreamUtils.copyToByteArray(request.getInputStream());
return new WrappedByteArrayInputStream(bytes);
} catch (IOException e) { } catch (IOException e) {
throw new IllegalStateException(e.getMessage(), e); throw new IllegalStateException(e.getMessage(), e);
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment