欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解决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

https://blog.csdn.net/ghw105745/article/details/107062103/

https://www.cnblogs.com/Sinte-Beuve/p/13260249.html

上一篇: JSON总结

下一篇: JSON总结