当前位置:网站首页>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) {
}
};
}
}
边栏推荐
猜你喜欢

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

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

机械臂速成小指南(七):机械臂位姿的描述方法

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

排序---

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

YYGH-9-预约下单

自然语言处理系列(一)——RNN基础

多文件程序X32dbg动态调试

How to Add P-Values onto Horizontal GGPLOTS
随机推荐
记录一下MySql update会锁定哪些范围的数据
YYGH-BUG-05
Differences between nodes and sharding in ES cluster
Codeforces 771-div2 C (trouble, permutation is not very good)
Leetcode122 买卖股票的最佳时机 II
测试左移和右移
From scratch, develop a web office suite (3): mouse events
Jenkins用户权限管理
XSS labs master shooting range environment construction and 1-6 problem solving ideas
Yygh-9-make an appointment to place an order
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
Experiment of connecting mobile phone hotspot based on Arduino and esp8266 (successful)
时间格式化显示
Log4j2
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
How to Visualize Missing Data in R using a Heatmap
Jenkins user rights management
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
How does Premiere (PR) import the preset mogrt template?
Cmake cross compilation