当前位置:网站首页>Post request body content cannot be retrieved repeatedly
Post request body content cannot be retrieved repeatedly
2022-07-02 12:09:00 【A cat wandering in the middle of the night】
post The request body content cannot be obtained repeatedly
Link to the original text https://zhhll.icu/2020/javaweb/ problem /6.post The request body content cannot be obtained repeatedly /
Why can't I read repeatedly ?
With tomcat For example , When reading the request body, the actual underlying call is org.apache.catalina.connector.Request Of getInputStream() Method , And the method returns CoyoteInputStream Input stream
public ServletInputStream getInputStream() throws IOException {
if (usingReader) {
throw new IllegalStateException(sm.getString("coyoteRequest.getInputStream.ise"));
}
usingInputStream = true;
if (inputStream == null) {
inputStream = new CoyoteInputStream(inputBuffer);
}
return inputStream;
}
In the use of CoyoteInputStream When reading
public int read(byte[] b, int off, int len) throws IOException {
// If the flow is closed , Throw an exception
if (closed) {
throw new IOException(sm.getString("inputBuffer.streamClosed"));
}
// If you've finished reading , Then return to -1
if (checkByteBufferEof()) {
return -1;
}
int n = Math.min(len, bb.remaining());
bb.get(b, off, n);
return n;
}
When the stream is read, it will proceed close, This flow close after ,close The state is set to true, Therefore, the stream cannot be read twice
So what's the solution ? take tomcat Of Request Class for re implementation ? It's too expensive ,sun The company has provided solutions when designing , For requests and responses ,sun The company provides packaging , Sure HttpServletRequestWrapper Class packaging original request object , Realized HttpServletRequest All methods of the interface , The wrapped request Object's corresponding method ; Correspondingly, there are HttpServletResponseWrapper Class to wrap the original response Object inheritance HttpServletRequestWrapper To rewrite methods , have access to HttpServletResponseWrapper and HttpServletRequestWrapper To customize responses and requests
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
// Store request body
private byte[] body;
private HttpServletRequest orgRequest;
public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
super(request);
this.orgRequest = request;
body = HttpHelper.getBody(request);
}
public HttpServletRequest getOrgRequest() {
return this.orgRequest;
}
// Rewrite read , Read from the stored byte array
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
// Rewrite read , Read from the stored byte array
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream bais = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public int read() throws IOException {
return bais.read();
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
};
}
}
边栏推荐
- ORB-SLAM2不同线程间的数据共享与传递
- to_ Bytes and from_ Bytes simple example
- Pytorch builds LSTM to realize clothing classification (fashionmnist)
- From scratch, develop a web office suite (3): mouse events
- Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
- Full link voltage measurement
- 高德地图测试用例
- HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
- Log4j2
- This article takes you to understand the operation of vim
猜你喜欢
From scratch, develop a web office suite (3): mouse events
ThreadLocal的简单理解
conda常用命令汇总
XSS labs master shooting range environment construction and 1-6 problem solving ideas
Natural language processing series (I) -- RNN Foundation
Jenkins voucher management
From scratch, develop a web office suite (3): mouse events
CONDA common command summary
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
使用Sqoop把ADS层数据导出到MySQL
随机推荐
排序---
Leetcode topic [array] -540- single element in an ordered array
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
YYGH-BUG-05
Leetcode739 每日温度
Find the factorial of a positive integer within 16, that is, the class of n (0= < n < =16). Enter 1111 to exit.
B high and beautiful code snippet sharing image generation
Log4j2
On data preprocessing in sklearn
Cmake cross compilation
Industry analysis
Read the Flink source code and join Alibaba cloud Flink group..
【C语言】十进制数转换成二进制数
How to Create a Beautiful Plots in R with Summary Statistics Labels
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
Differences between nodes and sharding in ES cluster
Larvel modify table fields
ThreadLocal的简单理解
Gaode map test case
From scratch, develop a web office suite (3): mouse events