当前位置:网站首页>getInputStream() has already been called for this request
getInputStream() has already been called for this request
2022-07-01 07:59:00 【Fate rushed out】
Recently, I am working on a logging system , There is a module inside that records the user's operation records , Need to get the request params, Back to result, And that is requestbody The content in , Then take body I found that I couldn't get it , This article records !
Wrong content
getInputStream() has already been called for this request
reason
ServletRequest Of getReader() and getInputStream() Two methods can only be called once , And you can't call both . So if Filter It was called once , stay Controller There can be no more calls inside
terms of settlement
First the Request Body preservation , And then through Servlet Self contained HttpServletRequestWrapper Class coverage getReader () and getInputStream () Method , Make the stream from the saved body Read . And then again Filter Lieutenant general ServletRequest Replace with MyRequestWrapper.
Add configuration class
RequestWrapper.java
package top.fate.config;
import org.apache.poi.util.IOUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/** * @auther:Wangxl * @Emile:[email protected] * @Time:2022/6/30 16:24 */
public class RequestWrapper extends HttpServletRequestWrapper {
private final byte[] body;
public RequestWrapper(HttpServletRequest request) throws IOException {
super(request);
body = IOUtils.toByteArray(request.getInputStream());
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(getInputStream()));
}
@Override
public ServletInputStream getInputStream() {
final ByteArrayInputStream bais = new ByteArrayInputStream(body);
return new ServletInputStream() {
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return false;
}
@Override
public void setReadListener(ReadListener readListener) {
}
@Override
public int read() {
return bais.read();
}
};
}
}
HttpServletRequestReplacedFilter.java
package top.fate.config;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
/** * @auther:Wangxl * @Emile:[email protected] * @Time:2022/6/30 16:35 */
public class HttpServletRequestReplacedFilter implements Filter {
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
ServletRequest servletWrapper = null;
if (servletRequest instanceof HttpServletRequest) {
servletWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
}
if (servletRequest == null) {
filterChain.doFilter(servletRequest, servletResponse);
} else {
try {
filterChain.doFilter(servletWrapper, servletResponse);
} catch (IOException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
}
}
}
}
边栏推荐
- uni 热更新
- 她就是那个「别人家的HR」|ONES 人物
- 【入门】取近似值
- 2022 test question bank and simulation test of tea master (primary) operation certificate
- Gdip - hatchbrush pattern table
- 【入门】输入n个整数,输出其中最小的k个
- How to use layui to display the data in the database in the form of tables
- 【力扣10天SQL入门】Day9 控制流
- Introduction to kubernetes resource objects and common commands (II)
- How to check ad user information?
猜你喜欢

Microsoft stream - how to modify video subtitles

凸印的印刷原理及工艺介绍
![[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario](/img/10/ed857892d2e0ea72e100a4008e6d69.png)
[recommendation system] breakthrough and imagination of deep location interactive network dpin for meituan takeout recommendation scenario

如何使用layui将数据库中的数据以表格的形式展现出来

base64
![[kv260] generate chip temperature curve with xadc](/img/fc/e5e4648b09b1123b2d494b75a9f8f7.png)
[kv260] generate chip temperature curve with xadc
![[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)](/img/48/de19e8cc007b93a027a906d4d423b2.png)
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)

Eigen matrix operation Library

H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?

ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
随机推荐
The H5 page has set the font thickness style, but the wechat access style in Huawei mobile phone doesn't take effect?
【入门】截取字符串
Insufficient executors to build thread pool
Cmake I two ways to compile source files
Php laraver Wechat payment
window c盘满了
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
Gru of RNN
2022 test questions and mock examinations for main principals of hazardous chemicals business units
力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
Scala语言学习-07-构造器
[website architecture] solve 90% of distributed transactions in one move, and introduce the working principles and application scenarios of database transactions and distributed transactions
Five combination boxing, solving six difficult problems on campus and escorting the construction of educational informatization
她就是那个「别人家的HR」|ONES 人物
[MySQL learning notes27] stored procedure
AArdio - 【问题】bass库回调时内存增长的问题
2022茶艺师(初级)操作证考试题库及模拟考试
【mysql学习笔记25】sql语句优化
如何让两融交易更极速
Introduction to kubernetes resource objects and common commands (II)