当前位置:网站首页>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) {
}
};
}
}
边栏推荐
- [untitled] how to mount a hard disk in armbian
- Leetcode14 longest public prefix
- 【C语言】十进制数转换成二进制数
- 自然语言处理系列(二)——使用RNN搭建字符级语言模型
- GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
- 自然语言处理系列(三)——LSTM
- CDA数据分析——Excel数据处理的常见知识点归纳
- The blink code based on Arduino and esp8266 runs successfully (including error analysis)
- Leetcode739 每日温度
- JZ63 股票的最大利润
猜你喜欢

How to Visualize Missing Data in R using a Heatmap

甜心教主:王心凌
![[C language] convert decimal numbers to binary numbers](/img/9b/1848b68b95d98389ed985c83f2e856.png)
[C language] convert decimal numbers to binary numbers

HR wonderful dividing line

自然语言处理系列(三)——LSTM

Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement

Mish shake the new successor of the deep learning relu activation function

HOW TO ADD P-VALUES TO GGPLOT FACETS

(C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.

初始JDBC 编程
随机推荐
自然语言处理系列(一)——RNN基础
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
The blink code based on Arduino and esp8266 runs successfully (including error analysis)
Log4j2
(C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
深入理解P-R曲线、ROC与AUC
YYGH-BUG-05
Data analysis - Matplotlib sample code
FastDateFormat为什么线程安全
ESP32 Arduino 引入LVGL 碰到的一些问题
Leetcode122 买卖股票的最佳时机 II
From scratch, develop a web office suite (3): mouse events
The most understandable f-string tutorial in history, collecting this one is enough
Leetcode209 subarray with the smallest length
Leetcode209 长度最小的子数组
Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
自然语言处理系列(二)——使用RNN搭建字符级语言模型
记录一下MySql update会锁定哪些范围的数据
B high and beautiful code snippet sharing image generation
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement