HttpServletRequestWrapper,HttpServletResponseWrapper的使用往往配合过滤器Filter使用,即 本文做一下记录一次用HttpServletResponseWrapper和拦截器来做输出压缩的过程
包装类
public class DatasWrapper extends HttpServletResponseWrapper {
private ByteArrayOutputStream outputStream;
public byte[] getResponseData(){
try {
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
return outputStream.toByteArray();
}
public DatasWrapper(HttpServletResponse response) {
super(response);
this.outputStream =new ByteArrayOutputStream();
}
@Override
public PrintWriter getWriter() throws IOException {
return new PrintWriter(outputStream);
}
@Override
public ServletOutputStream getOutputStream() throws IOException {
return new ServletOutputStream() {
@Override
public boolean isReady() {
return false;
}
@Override
public void setWriteListener(WriteListener listener) {
}
@Override
public void write(int b) throws IOException {
outputStream.write(b);
}
};
}
}
做拦截的类,我这里这个类只想对结果做一下压缩输出,当然开启压缩可以直接在tomcat等容器的参数配置上配置,这个是代码级别来压缩
@WebFilter(filterName="gzip",urlPatterns = "/getZip.do")
public class GzipFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
if (shouldFilte(servletRequest)){
DatasWrapper wrapper=new DatasWrapper((HttpServletResponse) servletResponse);
filterChain.doFilter(servletRequest,wrapper);
System.out.println("filter运行了");
byte[] responseData = wrapper.getResponseData();
byte[] gzip = gzip(responseData);
System.out.println("------------------");
System.out.println( "原来长度:"+responseData.length);
System.out.println("压缩后的长度:"+gzip.length);
System.out.println("--------------------");
HttpServletResponse response=(HttpServletResponse)servletResponse;
System.out.println(gzip.length);
System.out.println(Arrays.toString(gzip));
response.setHeader(HttpHeaders.CONTENT_ENCODING,"gzip");
response.setHeader(HttpHeaders.CONTENT_TYPE,"application/json;charset=utf-8");
response.setContentLength(gzip.length);
servletResponse.getOutputStream().write(gzip);
servletResponse.getOutputStream().flush();
}else {
filterChain.doFilter(servletRequest,servletResponse);
}
}
private byte[] gzip(byte[] responseData) throws IOException {
ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
GZIPOutputStream outputStream=new GZIPOutputStream(byteArrayOutputStream);
outputStream.write(responseData);
outputStream.finish();
outputStream.close();
byte[] bytes = byteArrayOutputStream.toByteArray();
byteArrayOutputStream.close();
return bytes;
}
private boolean shouldFilte(ServletRequest servletRequest) {
HttpServletRequest request=(HttpServletRequest)servletRequest;
String requestURI = request.getRequestURI();
if (requestURI.contains("/getZip.do")){
return true;
}
return false;
}
@Override
public void destroy() {
}
}
此处用了
@WebFilter注解,要想被扫描到,需要在运行主类上加上@ServletComponentScan
HttpServletRequestWrapper的用法差不多
参考:这一篇文章写的不错,可做参考 https://blog.csdn.net/u011410529/article/details/78873404