本篇内容介绍了“怎么构建可重复读取inputStream的request”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!构建可重复读取inpu
本篇内容介绍了“怎么构建可重复读取inputStream的request”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
我们知道,request的inputStream只能被读取一次,多次读取将报错,那么如何才能重复读取呢?答案之一是:增加缓冲,记录已读取的内容。
代码如下所示:
import lombok.extern.log4j.Log4j2;import org.springframework.mock.WEB.DelegatingServletInputStream;import javax.servlet.ServletInputStream;import javax.servlet.Http.httpservletRequest;import javax.servlet.http.HttpServletRequestWrapper;import java.io.*;@Log4j2public class RepeatedlyReadRequestWrapper extends HttpServletRequestWrapper { private static final int BUFFER_START_POSITION = 0; private static final int CHAR_BUFFER_LENGTH = 1024; private final String body; public RepeatedlyReadRequestWrapper(HttpServletRequest request) { super(request); StringBuilder stringBuilder = new StringBuilder(); InputStream inputStream = null; try { inputStream = request.getInputStream(); } catch (IOException e) { log.error("Error reading the request body…", e); } if (inputStream != null) { try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))) { char[] charBuffer = new char[CHAR_BUFFER_LENGTH]; int bytesRead; while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { stringBuilder.append(charBuffer, BUFFER_START_POSITION, bytesRead); } } catch (IOException e) { log.error("Fail to read input stream",e); } } else { stringBuilder.append(""); } body = stringBuilder.toString(); } @Override public ServletInputStream getInputStream() throws IOException { final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body.getBytes()); return new DelegatingServletInputStream(byteArrayInputStream); }}
接下来,需要一个对应的Filter.
代码如下所示:
import javax.servlet.*;import javax.servlet.http.HttpServletRequest;import java.io.IOException;public class RepeatlyReadFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { //Do nothing } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest) { request = new RepeatedlyReadRequestWrapper((HttpServletRequest) request); } chain.doFilter(request, response); } @Override public void destroy() { //Do nothing }}
最后,需要在web.xml中,增加该Filter的配置(略)。
在使用HTTP协议实现应用间接口通信时,服务端读取客户端请求过来的数据,会用到request.getInputStream(),第一次读取的时候可以读取到数据,但是接下来的读取操作都读取不到数据。
一个InputStream对象在被读取完成后,将无法被再次读取,始终返回-1;
InputStream并没有实现reset方法(可以重置首次读取的位置),无法实现重置操作;
使用request、session等来缓存读取到的数据,这种方式很容易实现,只要setAttribute和getAttribute就行;
使用HttpServletRequestWrapper来包装HttpServletRequest,在中初始化读取request的InputStream数据,以byte[]形式缓存在其中,然后在Filter中将request转换为包装过的request;
编写rHttpServletRequestWrapper子类,用来处理请求数据
import java.io.BufferedReader;import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.NIO.charset.Charset;import java.util.Enumeration;import javax.servlet.ReadListener;import javax.servlet.ServletInputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import lombok.extern.slf4j.Slf4j;@Slf4jpublic class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper{private final byte[] body;public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException{super(request);Enumeration<String> e = request.getHeaderNames();while (e.hasMoreElements()){String name = (String) e.nextElement();String value = request.getHeader(name);log.debug("HttpServletRequest头信息:{}-{}", name, value);}body = HttpHelper.getBodyString(request).getBytes(Charset.forName("UTF-8"));}@Overridepublic BufferedReader getReader() throws IOException{return new BufferedReader(new InputStreamReader(getInputStream()));}@Overridepublic ServletInputStream getInputStream() throws IOException{final ByteArrayInputStream bais = new ByteArrayInputStream(body);return new ServletInputStream(){@Overridepublic boolean isFinished(){return false;}@Overridepublic boolean isReady(){return false;}@Overridepublic void setReadListener(ReadListener listener){}@Overridepublic int read() throws IOException{return bais.read();}};}@Overridepublic String getHeader(String name){return super.getHeader(name);}@Overridepublic Enumeration<String> getHeaderNames(){return super.getHeaderNames();}@Overridepublic Enumeration<String> getHeaders(String name){return super.getHeaders(name);}}
调用
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException{HttpServletRequest httpRequest = (HttpServletRequest) request;HttpServletResponse httpResponse = (HttpServletResponse) response;ServletRequest requestWrapper = null;requestWrapper = new BodyReaderHttpServletRequestWrapper(httpRequest);//数据读取处理//...//将requestWrapper专递给后面的过滤器filterChain.doFilter(requestWrapper, httpResponse);}
“怎么构建可重复读取inputStream的request”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
--结束END--
本文标题: 怎么构建可重复读取inputStream的request
本文链接: https://lsjlt.com/news/324806.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0