当前位置:网站首页>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 Visualize Missing Data in R using a Heatmap
- xss-labs-master靶场环境搭建与1-6关解题思路
- SVO2系列之深度濾波DepthFilter
- Log4j2
- YYGH-BUG-05
- Implementation of address book (file version)
- HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
- HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
- 【工控老马】西门子PLC Siemens PLC TCP协议详解
- 史上最易懂的f-string教程,收藏这一篇就够了
猜你喜欢

XSS labs master shooting range environment construction and 1-6 problem solving ideas

5g era, learning audio and video development, a super hot audio and video advanced development and learning classic

HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R

GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R

ES集群中节点与分片的区别

HR wonderful dividing line

测试左移和右移

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

H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser

K-Means Clustering Visualization in R: Step By Step Guide
随机推荐
Leetcode topic [array] -540- single element in an ordered array
[geek challenge 2019] upload
Leetcode209 长度最小的子数组
Uniapp uni list item @click, uniapp uni list item jump with parameters
HR wonderful dividing line
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
YYGH-10-微信支付
刷题---二叉树--2
Orb-slam2 data sharing and transmission between different threads
File operation (detailed!)
史上最易懂的f-string教程,收藏這一篇就够了
Leetcode122 买卖股票的最佳时机 II
Tas (file d'attente prioritaire)
字符串回文hash 模板题 O(1)判字符串是否回文
From scratch, develop a web office suite (3): mouse events
(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?
Gaode map test case
PHP 2D and multidimensional arrays are out of order, PHP_ PHP scrambles a simple example of a two-dimensional array and a multi-dimensional array. The shuffle function in PHP can only scramble one-dim
多文件程序X32dbg动态调试
SVO2系列之深度濾波DepthFilter