当前位置:网站首页>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();
}
}
}
}
边栏推荐
- Li Kou daily question - day 31 -1790 Can a string exchange be performed only once to make two strings equal
- 【入门】输入整型数组和排序标识,对其元素按照升序或降序进行排序
- Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
- MATLAB之基础知识
- How do the top ten securities firms open accounts? In addition, is it safe to open a mobile account?
- 【R语言】两个/N个数据合并merge函数
- 038 network security JS
- Latex table
- Gui Gui programming (XV) - use scale to control font size changes
- 力扣每日一题-第31天-1790.仅执行一次字符串交换能否使两个字符串相等
猜你喜欢
2022 mobile crane driver test exercises and online simulation test
2022危险化学品经营单位主要负责人试题及模拟考试
Set up file server Minio for quick use
【批处理DOS-CMD命令-汇总和小结】-Cmd窗口中常用操作符(<、<<、&<、>、>>、&>、&、&&、||、|、()、;、@)
LSTM of RNN
2022年茶艺师(中级)复训题库及答案
图扑软件通过 CMMI5 级认证!| 国际软件领域高权威高等级认证
Array: question brushing record
2022茶艺师(初级)操作证考试题库及模拟考试
H5 页面设置了字体的粗细样式,但是在华为手机里微信打开访问样式不生效?
随机推荐
力扣每日一题-第31天-202.快乐数
Cyclic neural network
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Oracle create auto increment ID
Source code analysis of open source API gateway APIs IX
2022 electrician (intermediate) recurrent training question bank and answers
Conscience Amway universal wheel SolidWorks model material website
Latex table
2022 test questions and mock examinations for main principals of hazardous chemicals business units
2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
PostgreSQL source code learning (26) -- windows vscode remote debugging PostgreSQL on Linux
她就是那个「别人家的HR」|ONES 人物
ONES 创始人王颖奇对话《财富》(中文版):中国有没有优秀的软件?
[batch DOS CMD summary] extension variables - delay variables CMD /v:on, CMD /v:off, SETLOCAL enabledelayedexpansion, disabledelayedexpansion
Vhost kick & call principle
What information does the supplier need to know about Audi EDI project?
Aardio - Shadow Gradient Text
I bet on performance and won the CTO of the company. I want to build Devops platform!
[MySQL learning notes 28] storage function
【入门】提取不重复的整数