当前位置:网站首页>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) {
}
};
}
}
边栏推荐
- FastDateFormat为什么线程安全
- Gaode map test case
- Full link voltage measurement
- 记录一下MySql update会锁定哪些范围的数据
- How does Premiere (PR) import the preset mogrt template?
- CONDA common command summary
- Time format display
- Some problems encountered in introducing lvgl into esp32 Arduino
- PyTorch nn.RNN 参数全解析
- Deep understanding of P-R curve, ROC and AUC
猜你喜欢
堆(优先级队列)
How does Premiere (PR) import the preset mogrt template?
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
conda常用命令汇总
Differences between nodes and sharding in ES cluster
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
ES集群中节点与分片的区别
Natural language processing series (I) -- RNN Foundation
(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。
多文件程序X32dbg动态调试
随机推荐
Filtre de profondeur de la série svo2
Yygh-9-make an appointment to place an order
[untitled] how to mount a hard disk in armbian
Input a three digit number and output its single digit, ten digit and hundred digit.
HR wonderful dividing line
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
jenkins 凭证管理
Natural language processing series (III) -- LSTM
CMake交叉编译
How to Visualize Missing Data in R using a Heatmap
Pytorch builds LSTM to realize clothing classification (fashionmnist)
CDA数据分析——Excel数据处理的常见知识点归纳
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
YYGH-BUG-04
From scratch, develop a web office suite (3): mouse events
Leetcode122 买卖股票的最佳时机 II
conda常用命令汇总
还不会安装WSL 2?看这一篇文章就够了
PyTorch nn.RNN 参数全解析
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?