• 多次读取post请求中的流信息


    package com.voole.config.web;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.nio.charset.Charset;
    
    import javax.servlet.ServletRequest;
    
    public class HttpHelper {
    	
    	 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) {
    	            e.printStackTrace();
    	        } 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();
    	    }
    }
    

     这个是辅助的工具类

     

    package com.voole.config.web;
    
    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;
    
    public class BodyHttpServletRequestWrapper extends HttpServletRequestWrapper {
    
    	private final byte[] body;
    
    	public BodyHttpServletRequestWrapper(HttpServletRequest request) {
    		super(request);
    		Enumeration<String> e = request.getHeaderNames();
    
    		while (e.hasMoreElements()) {
    			String name = e.nextElement();
    			String value = request.getHeader(name);
    		}
    		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 void setReadListener(ReadListener listener) {
    				
    			}
    			
    			@Override
    			public boolean isReady() {
    				return false;
    			}
    			
    			@Override
    			public boolean isFinished() {
    				return false;
    			}
    		};
    	}
    
    }
    

      对request的封装

    package com.voole.config.web;
    
    import java.io.IOException;
    
    import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.ServletException;
    import javax.servlet.ServletInputStream;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;
    import javax.servlet.annotation.WebFilter;
    import javax.servlet.http.HttpServletRequest;
    
    import org.apache.commons.io.IOUtils;
    import org.springframework.stereotype.Component;
    
    @Component
    @WebFilter(urlPatterns="/*",filterName="vooleFilter")
    public class VooleFilter implements Filter{
    	
    
    	@Override
    	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
    			throws IOException, ServletException {
    		HttpServletRequest req = (HttpServletRequest)request;
    		BodyHttpServletRequestWrapper body = new BodyHttpServletRequestWrapper(req);
    		System.out.println(req.getRequestURI());
    		String appid = req.getParameter("appid");
    		System.out.println("
    VooleFilter appid "+appid);
    		System.out.println(IOUtils.toString(body.getInputStream()));
    		chain.doFilter(body, response);
    	}
    
    }
    

      在过滤器中获取后,后续使用的都是封装后的request

  • 相关阅读:
    HTTP
    HTTP
    Tomcat
    Tomcat
    时下流行的9种恶意软件,你都了解吗?
    MYSQL中表级锁、行级锁、页级锁介绍
    MYSQL中表级锁、行级锁、页级锁介绍
    MYSQL存储引擎笔记
    MYSQL存储引擎笔记
    Shell脚本案例:批量新增用户
  • 原文地址:https://www.cnblogs.com/nihaofenghao/p/10444181.html
Copyright © 2020-2023  润新知