spring boot 过滤器实现接收 压缩数据 并解压

1.新加类GzipRequestWrapper 继承HttpServletRequestWrapper类

public class GzipRequestWrapper  extends HttpServletRequestWrapper{

    private HttpServletRequest request;  
    private static final Logger LOGGER = LoggerFactory.getLogger(GzipRequestWrapper.class); 

    public GzipRequestWrapper(HttpServletRequest request) {  
        super(request);  
        this.request = request;  
    } 


    @Override  
    public ServletInputStream getInputStream() throws IOException {  
        
        ServletInputStream stream = request.getInputStream();  
        
        String contentEncoding = request.getHeader("Content-Encoding");  
        // 如果对内容进行了压缩,则解压  
        if (null != contentEncoding && contentEncoding.indexOf("gzip") != -1) {  
            try {  
                final GZIPInputStream gzipInputStream = new GZIPInputStream(  
                        stream);  

                ServletInputStream newStream = new ServletInputStream() {  

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

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

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

                    @Override
                    public void setReadListener(ReadListener arg0) {
                        // TODO Auto-generated method stub
                        
                    }  
                };  
                return newStream;  
            } catch (Exception e) {  
                LOGGER.debug("ungzip content fail.", e);  
            }  
        }  
        return stream;  
    }  


}

2.新加FilterGzip 实现 Filter(注意使用注解)

@WebFilter(filterName="AccessFilter",urlPatterns="/")
public class FilterGzip implements Filter {

    @Override
    public void destroy() {
        
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        
        System.out.println("filter");

        chain.doFilter(new GzipRequestWrapper((HttpServletRequest) request),  
                response);
        
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException {
        
    }

}

3.在启动类添加

@Bean  
    public FilterRegistrationBean  filterRegistrationBean() {  
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();  
        FilterGzip httpBasicFilter = new FilterGzip();  
        registrationBean.setFilter(httpBasicFilter);  
        List<String> urlPatterns = new ArrayList<String>();  
        urlPatterns.add("/*");  
        registrationBean.setUrlPatterns(urlPatterns);  
        return registrationBean;  
    } 

4.使用

@RequestMapping(value="/info")
    public String receive(HttpServletRequest req){
        
        StringBuilder sb = null;
        String json = null;
        try {
            ServletInputStream inputStream = req.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,"UTF-8"));   
            sb = new StringBuilder();   

            String line = null;   
            while ((line = reader.readLine()) != null) {   
                sb.append(line);   
            }
            json = URLEncoder.encode(sb.toString(), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }  
        System.out.println("sb ----> " + sb.toString());
        if(sb != null){
            logger.info("**** 接收成功1条 ****");
        }
        MultiValueMap<String, String> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("jsonStr", json);
        restTemplate.postForObject("http://SERVICE-DATA/pretreat", paramMap, String.class);
        
        logger.info("#### 传输 成功!####");
        
        return "ok";
    }

 

posted @ 2017-06-28 17:27  丨Mars  阅读(...)  评论(... 编辑 收藏