当前位置:网站首页>post请求体内容无法重复获取
post请求体内容无法重复获取
2022-07-02 09:42:00 【徘徊在深夜中的猫】
post请求体内容无法重复获取
原文链接 https://zhhll.icu/2020/javaweb/问题/6.post请求体内容无法重复获取/
为什么会无法重复读取呢?
以tomcat为例,在进行请求体读取时实际底层调用的是org.apache.catalina.connector.Request的getInputStream()方法,而该方法返回的是CoyoteInputStream输入流
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;
}
在使用CoyoteInputStream进行读取时
public int read(byte[] b, int off, int len) throws IOException {
// 如果流关闭,则抛出异常
if (closed) {
throw new IOException(sm.getString("inputBuffer.streamClosed"));
}
// 如果已经读完了,则返回-1
if (checkByteBufferEof()) {
return -1;
}
int n = Math.min(len, bb.remaining());
bb.get(b, off, n);
return n;
}
而流读取完毕都会进行close,这个流close之后,close状态就置为了true,所以导致流无法进行二次读取
那么如何解决呢?将tomcat的Request类进行重新实现吗?代价太大了,sun公司当初在设计的时候就已经提供了解决方法,对于请求和响应,sun公司提供了包装类,可以HttpServletRequestWrapper类包装原始的request对象,实现了HttpServletRequest接口的所有方法,内部调用了所包装的request对象的对应方法;相应的也有HttpServletResponseWrapper类来包装原始的response对象继承HttpServletRequestWrapper来进行方法重写,可以使用HttpServletResponseWrapper和HttpServletRequestWrapper来进行定制响应和请求
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {
// 存储请求体
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;
}
// 重写读取,从存储的字节数组中读
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
// 重写读取,从存储的字节数组中读
@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) {
}
};
}
}
边栏推荐
- Natural language processing series (III) -- LSTM
- Applet link generation
- QT meter custom control
- XSS labs master shooting range environment construction and 1-6 problem solving ideas
- YYGH-BUG-04
- GGPLOT: HOW TO DISPLAY THE LAST VALUE OF EACH LINE AS LABEL
- to_ Bytes and from_ Bytes simple example
- Flesh-dect (media 2021) -- a viewpoint of material decomposition
- to_bytes与from_bytes简单示例
- 自然语言处理系列(一)——RNN基础
猜你喜欢
Applet link generation
Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
Dynamic memory (advanced 4)
基于Arduino和ESP8266的Blink代码运行成功(包含错误分析)
The computer screen is black for no reason, and the brightness cannot be adjusted.
BEAUTIFUL GGPLOT VENN DIAGRAM WITH R
Yygh-9-make an appointment to place an order
Seriation in R: How to Optimally Order Objects in a Data Matrice
Tiktok overseas tiktok: finalizing the final data security agreement with Biden government
随机推荐
通讯录的实现(文件版本)
Mish shake the new successor of the deep learning relu activation function
Fabric.js 3个api设置画布宽高
b格高且好看的代码片段分享图片生成
Take you ten days to easily finish the finale of go micro services (distributed transactions)
高德地图测试用例
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
How to Create a Nice Box and Whisker Plot in R
深入理解PyTorch中的nn.Embedding
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Log4j2
HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R
easyExcel和lombok注解以及swagger常用注解
Yygh-10-wechat payment
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
Codeforces 771-div2 C (trouble, permutation is not very good)
From scratch, develop a web office suite (3): mouse events
浅谈sklearn中的数据预处理
PyTorch搭建LSTM实现服装分类(FashionMNIST)
PHP query distance according to longitude and latitude