当前位置:网站首页>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) {
}
};
}
}
边栏推荐
- qt 仪表自定义控件
- easyExcel和lombok注解以及swagger常用注解
- 进入前六!博云在中国云管理软件市场销量排行持续上升
- Data analysis - Matplotlib sample code
- How to Create a Beautiful Plots in R with Summary Statistics Labels
- PX4 Position_ Control RC_ Remoter import
- Leetcode topic [array] -540- single element in an ordered array
- 二分刷题记录(洛谷题单)区间的甄别
- ORB-SLAM2不同线程间的数据共享与传递
- JZ63 股票的最大利润
猜你喜欢
pgsql 字符串转数组关联其他表,匹配 拼接后原顺序展示
How does Premiere (PR) import the preset mogrt template?
How to Add P-Values onto Horizontal GGPLOTS
Natural language processing series (I) -- RNN Foundation
[visual studio 2019] create MFC desktop program (install MFC development components | create MFC application | edit MFC application window | add click event for button | Modify button text | open appl
Applet link generation
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Esp32 audio frame esp-adf add key peripheral process code tracking
How to Create a Beautiful Plots in R with Summary Statistics Labels
[geek challenge 2019] upload
随机推荐
Power Spectral Density Estimates Using FFT---MATLAB
Dynamic memory (advanced 4)
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
进入前六!博云在中国云管理软件市场销量排行持续上升
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
HOW TO EASILY CREATE BARPLOTS WITH ERROR BARS IN R
史上最易懂的f-string教程,收藏這一篇就够了
SVO2系列之深度滤波DepthFilter
数据分析 - matplotlib示例代码
Pyqt5+opencv project practice: microcirculator pictures, video recording and manual comparison software (with source code)
CMake交叉编译
Orb-slam2 data sharing and transmission between different threads
H5, add a mask layer to the page, which is similar to clicking the upper right corner to open it in the browser
YYGH-10-微信支付
求16以内正整数的阶乘,也就是n的阶层(0=<n<=16)。输入1111退出。
Implementation of address book (file version)
Tiktok overseas tiktok: finalizing the final data security agreement with Biden government
【C语言】十进制数转换成二进制数
HR wonderful dividing line
uniapp uni-list-item @click,uniapp uni-list-item带参数跳转