解决HttpServletRequest流只能读取一次
程序员文章站
2022-03-01 15:44:32
...
import org.apache.tomcat.util.http.fileupload.IOUtils;
import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.*;
public class ReadTwiceHttpServletRequestWrapper extends HttpServletRequestWrapper {
private ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
public ReadTwiceHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
try {
request.setCharacterEncoding("UTF-8");
IOUtils.copy(request.getInputStream(), outputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public BufferedReader getReader() throws IOException {
return new BufferedReader(new InputStreamReader(new ByteArrayInputStream(outputStream.toByteArray())));
}
@Override
public ServletInputStream getInputStream() throws IOException {
final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
return new ServletInputStream() {
@Override
public int readLine(byte[] b, int off, int len) throws IOException {
return inputStream.read(b, off, len);
}
@Override
public boolean isFinished() {
return inputStream.available() > 0;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener arg0) {
// TODO Auto-generated method stub
}
@Override
public int read() throws IOException {
return inputStream.read();
}
};
}
public void setBody(String body) {
outputStream = new ByteArrayOutputStream();
try {
outputStream.write(body.getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if(outputStream != null){
try{
outputStream.close();
}catch (Exception ee){
ee.printStackTrace();
}
}
}
}
public String getBody() {
String str = new String(outputStream.toByteArray());
if(outputStream != null){
try{
outputStream.close();
}catch (Exception e){
e.printStackTrace();
}
}
return str;
}
}
package com.wantup.filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
public class ReadTwiceFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
ReadTwiceHttpServletRequestWrapper readTwiceHttpServletRequestWrapper = new ReadTwiceHttpServletRequestWrapper(
(HttpServletRequest) request);
String newBody = readTwiceHttpServletRequestWrapper.getBody();
readTwiceHttpServletRequestWrapper.setBody(newBody);
chain.doFilter(readTwiceHttpServletRequestWrapper, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
}
}
@Bean
public FilterRegistrationBean indexFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean(new ReadTwiceFilter());
return registration;
}
参考链接:
https://blog.csdn.net/weixin_41973131/article/details/84956793
推荐阅读
-
C#实现微信公众号群发消息(解决一天只能发一次的限制)实例分享
-
“您一次只能安装一种 Adobe 产品”的解决办法
-
浅谈request.getinputstream只能读取一次的问题
-
“您一次只能安装一种 Adobe 产品”的解决办法
-
inputstream 解决只能读取一次
-
有一个函数是读取mysql的资源 是一次读取全部 不用循环的函数!解决方法
-
用ADO更新MYSQL报“无法为更新定位行。一些值可能已在最后一次读取后已更改”问题的解决_MySQL
-
用ADO更新MYSQL报“无法为更新定位行。一些值可能已在最后一次读取后已更改”问题的解决_MySQL
-
JavaScript-RegExp对象只能使用一次问题解决方法_javascript技巧
-
有一个函数是读取mysql的资源 是一次读取全部 不用循环的函数!解决方法