当前位置:网站首页>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();
}
}
}
}
边栏推荐
- QT -- 1. QT connection database
- SharePoint - modify web application authentication using PowerShell
- How do the top ten securities firms open accounts? In addition, is it safe to open a mobile account?
- 凸印的印刷原理及工艺介绍
- getInputStream() has already been called for this request
- How outlook puts together messages with the same discussion
- Introduction to kubernetes resource objects and common commands (II)
- SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?
- Caesar
- Significance and measures of source code encryption
猜你喜欢

How to check ad user information?

如何让两融交易更极速

2022 Guangdong Provincial Safety Officer a certificate third batch (main person in charge) special operation certificate examination question bank simulated examination platform operation

SharePoint - how to quickly check whether SharePoint is standard or enterprise edition?

Minecraft 1.16.5 module development (51) tile entity

軟鍵盤高度報錯

Aardio - 阴影渐变文字

5大组合拳,解决校园6大难题,护航教育信息化建设

Conscience Amway universal wheel SolidWorks model material website

Wang Yingqi, founder of ones, talks to fortune (Chinese version): is there any excellent software in China?
随机推荐
Insufficient executors to build thread pool
Aardio - [problem] the problem of memory growth during the callback of bass Library
【mysql学习笔记25】sql语句优化
How to get a SharePoint online site created using the office365 group template
[batch dos-cmd command - summary and summary] - Common operators in the CMD window (<, < <, & <,>, > >, & >, & >, & &, ||, (),;, @)
2022 operation of refrigeration and air conditioning equipment operation of national question bank simulated examination platform
Aardio - Shadow Gradient Text
下载Xshell和Xftp
2022 tea master (intermediate) recurrent training question bank and answers
Sorting out tcp/udp communication problems
【R语言】年龄性别频数匹配 挑选样本 病例对照研究,对年龄性别进行频数匹配
Chinese font Gan: zi2zi
How outlook puts together messages with the same discussion
postgresql源码学习(26)—— Windows vscode远程调试Linux上的postgresql
【mysql学习笔记27】存储过程
Cmake I two ways to compile source files
Scala语言学习-07-构造器
Atguigu---- scaffold --02- use scaffold (2)
力扣——求一组字符中的第一个回文字符
uni 热更新