当前位置:网站首页>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) {
}
};
}
}
边栏推荐
- How to Add P-Values onto Horizontal GGPLOTS
- 堆(優先級隊列)
- Yygh-9-make an appointment to place an order
- How does Premiere (PR) import the preset mogrt template?
- Mish shake the new successor of the deep learning relu activation function
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- [geek challenge 2019] upload
- Mish-撼动深度学习ReLU激活函数的新继任者
- 浅谈sklearn中的数据预处理
- Deep understanding of P-R curve, ROC and AUC
猜你喜欢

GGPlot Examples Best Reference

Power Spectral Density Estimates Using FFT---MATLAB

Take you ten days to easily finish the finale of go micro services (distributed transactions)

Dynamic memory (advanced 4)

HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE

Natural language processing series (III) -- LSTM

自然语言处理系列(一)——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?

Mish shake the new successor of the deep learning relu activation function
随机推荐
CONDA common command summary
YYGH-BUG-04
YYGH-BUG-04
时间格式化显示
Leetcode209 长度最小的子数组
甜心教主:王心凌
Seriation in R: How to Optimally Order Objects in a Data Matrice
Log4j2
drools决策表的简单使用
多文件程序X32dbg动态调试
使用Sqoop把ADS层数据导出到MySQL
drools执行String规则或执行某个规则文件
刷题---二叉树--2
Depth filter of SvO2 series
Filtre de profondeur de la série svo2
记录一下MySql update会锁定哪些范围的数据
Dynamic memory (advanced 4)
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
From scratch, develop a web office suite (3): mouse events
Input a three digit number and output its single digit, ten digit and hundred digit.