当前位置:网站首页>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) {
}
};
}
}
边栏推荐
- The computer screen is black for no reason, and the brightness cannot be adjusted.
- Beautiful and intelligent, Haval H6 supreme+ makes Yuanxiao travel safer
- pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
- ESP32 Arduino 引入LVGL 碰到的一些问题
- Lekao: contents of the provisions on the responsibility of units for fire safety in the fire protection law
- Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
- Repeat, tile and repeat in pytorch_ The difference between interleave
- [geek challenge 2019] upload
- SVO2系列之深度滤波DepthFilter
- 数据分析 - matplotlib示例代码
猜你喜欢

Pytorch builds LSTM to realize clothing classification (fashionmnist)

R HISTOGRAM EXAMPLE QUICK REFERENCE

Dynamic memory (advanced 4)

Fresh, 2022 advanced Android interview must know 100 questions (interview questions + answer analysis)

HOW TO CREATE AN INTERACTIVE CORRELATION MATRIX HEATMAP IN R

Seriation in R: How to Optimally Order Objects in a Data Matrice

小程序链接生成

xss-labs-master靶场环境搭建与1-6关解题思路

还不会安装WSL 2?看这一篇文章就够了

Tiktok overseas tiktok: finalizing the final data security agreement with Biden government
随机推荐
史上最易懂的f-string教程,收藏這一篇就够了
The selected cells in Excel form have the selection effect of cross shading
[QT] Qt development environment installation (QT version 5.14.2 | QT download | QT installation)
ORB-SLAM2不同线程间的数据共享与传递
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
测试左移和右移
YYGH-10-微信支付
【2022 ACTF-wp】
[geek challenge 2019] upload
Uniapp uni list item @click, uniapp uni list item jump with parameters
Flesh-dect (media 2021) -- a viewpoint of material decomposition
GGPUBR: HOW TO ADD ADJUSTED P-VALUES TO A MULTI-PANEL GGPLOT
【C语言】十进制数转换成二进制数
CMake交叉编译
自然语言处理系列(三)——LSTM
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Codeforces 771 div2 B (no one FST, refers to himself)
Applet link generation
高德地图测试用例