为了账号安全,请及时绑定邮箱和手机立即绑定

如何多次读取request.getInputStream()

如何多次读取request.getInputStream()

慕斯王 2019-10-30 12:56:39
我有以下代码:@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)        throws IOException, ServletException {    logger.info("Filter start...");    HttpServletRequest httpRequest = (HttpServletRequest) request;    HttpServletResponse httpResponse = (HttpServletResponse) response;    String ba = getBaId(getBody(httpRequest));    if (ba == null) {        logger.error("Wrong XML");        httpResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);    } else {              if (!clients.containsKey(ba)) {            clients.put(ba, 1);            logger.info("Client map : init...");        } else {            clients.put(ba, clients.get(ba).intValue() + 1);            logger.info("Threads for " + ba + " = " + clients.get(ba).toString());        }        chain.doFilter(request, response);    }}和此web.xml(缩短了程序包并更改了名称,但外观相同)<?xml version="1.0" encoding="ISO-8859-1"?><web-app>  <filter>    <filter-name>TestFilter</filter-name>    <filter-class>pkg.TestFilter</filter-class>  </filter>  <filter-mapping>    <filter-name>TestFilter</filter-name>    <url-pattern>/*</url-pattern>  </filter-mapping>  <context-param>    <param-name>contextConfigLocation</param-name>    <param-value>WEB-INF/applicationContext.xml</param-value>  </context-param>  <listener>    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>  <servlet>    <servlet-name>Name</servlet-name>    <display-name>Name</display-name>    <servlet-class>pkg.Name</servlet-class>    <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>    <servlet-name>Name</servlet-name>    <url-pattern>/services/*</url-pattern>  </servlet-mapping></web-app>我想在过滤器之后调用Servlet。我希望chain.doFilter(...)可以做到这一点,但是我总是会遇到以下错误chain.doFilter(...)
查看完整描述

3 回答

?
千巷猫影

TA贡献1829条经验 获得超7个赞

您可能开始使用getReader()in 使用HttpServletRequest :


String ba = getBaId(getBody(httpRequest)); 

您的servlet尝试调用getInputStream()相同的请求,这是不允许的。您需要做的是使用ServletRequestWrapper制作请求正文的副本,因此您可以使用多种方法读取它。我没有时间找到完整的示例,对吧...对不起...


查看完整回答
反对 回复 2019-10-30
  • 3 回答
  • 0 关注
  • 1611 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信