解决request.getInputStream()只能读取一次的问题

request.getInputStream()只能够读取一次,但是在实际的开发中往往需要多次读取。这样就需要我们将流写入进去,提供后续使用。方法可能有很多,
这里提供其中一种!
1:创建HttpHelper类获取流中的数据。
 
  
public class HttpHelper {

    /**
     * 获取请求Body
     * @param request
     * @return
     */
    private static Logger logger = LoggerFactory.getLogger(HttpHelper.class);
    public static String getBodyString(ServletRequest request) {
        StringBuilder sb = new StringBuilder();
        InputStream inputStream = null;
        BufferedReader reader = null;
        try {
            inputStream = request.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            logger.warn("getBodyString出现问题!");
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }
}
2:重写HttpServletRequestWrapper类
 
  
public class BodyReaderHttpServletRequestWrapper extends HttpServletRequestWrapper {

    private final byte[] body;

    public BodyReaderHttpServletRequestWrapper(HttpServletRequest request) throws IOException {
        super(request);
        body = HttpHelper.getBodyString(request).getBytes(Charset.forName("UTF-8"));
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {

        final ByteArrayInputStream bais = new ByteArrayInputStream(body);

        return new ServletInputStream() {

            @Override
            public int read() throws IOException {
                return bais.read();
            }

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }
        };
    }
}
3:在过滤器中加入如下代码
 
  
@Configuration
public class CorsConfig extends OncePerRequestFilter {

    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {

        // 防止流读取一次后就没有了, 所以需要将流继续写出去,提供后续使用
        ServletRequest requestWrapper = new BodyReaderHttpServletRequestWrapper(request);
        String json = HttpHelper.getBodyString(requestWrapper);
        filterChain.doFilter(requestWrapper, response);
    }
}
这里一定要注意返回的request必须是我们重写之后的,否则还是会出现问题!

 
  

你可能感兴趣的:(spring,boot)