Commit 9225626f authored by liaozan's avatar liaozan 🏀

Fix the stream closed error

parent 1bd7ef80
......@@ -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);
}
......
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