package cn.hongxin.filter;public class GzipFilter2 implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse resp= (HttpServletResponse) response; //声明包装类对象 MyResponse2 myresp = new MyResponse2(resp); //放行,调用oneServlet.doGet chain.doFilter(request, myresp); //第二步:从myresp2中读取原生的数据 byte[] src = myresp.getSrc(); //第三步:开始压缩 ByteArrayOutputStream destBytes = new ByteArrayOutputStream(); GZIPOutputStream zip = new GZIPOutputStream(destBytes); zip.write(src); zip.close(); //第三步:输出-使用原生的response resp.setContentType("text/html;charset=UTF-8"); //获压缩以后数据 byte[] dest = destBytes.toByteArray(); System.err.PRintln("压缩之前:"+src.length); System.err.println("压缩以后:"+dest.length); //设置头 resp.setContentLength(dest.length); resp.setHeader("Content-Encoding","gzip"); //输出 OutputStream out = resp.getOutputStream(); out.write(dest); } public void destroy() { }}
//第一步:声明response的包装类class MyResponse2 extends HttpServletResponseWrapper{ //将这个容器/a.txt,声明成员变量 private ByteArrayOutputStream srcByte; public MyResponse2(HttpServletResponse response) { super(response); } //修改增强getWtier方法 @Override public PrintWriter getWriter() throws IOException { srcByte = new ByteArrayOutputStream(); PrintWriter out = new PrintWriter( new OutputStreamWriter(srcByte, "UTF-8")); return out; } //提供一个方法获取原生 的数据 public byte[] getSrc(){ return srcByte.toByteArray(); }}第五步:全部的jsp都要经过压缩
只要是通过包装rersponse,且修改了getWriter方法,返回一个自己的printwiter对象。声明一个放原数据的容器对象。就可以实现数据压缩。
public class GzipFilter2 implements Filter { public void init(FilterConfig filterConfig) throws ServletException { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletResponse resp= (HttpServletResponse) response; //声明包装类对象 MyResponse2 myresp = new MyResponse2(resp); //放行,调用oneServlet.doGet chain.doFilter(request, myresp); //第二步:从myresp2中读取原生的数据 byte[] src = myresp.getSrc(); //第三步:开始压缩 ByteArrayOutputStream destBytes = new ByteArrayOutputStream(); GZIPOutputStream zip = new GZIPOutputStream(destBytes); zip.write(src); zip.close(); //第三步:输出-使用原生的response resp.setContentType("text/html;charset=UTF-8"); //获压缩以后数据 byte[] dest = destBytes.toByteArray(); System.err.println("压缩之前:"+src.length); System.err.println("压缩以后:"+dest.length); //设置头 resp.setContentLength(dest.length); resp.setHeader("Content-Encoding","gzip"); //输出 OutputStream out = resp.getOutputStream(); out.write(dest); } public void destroy() { }}
//第一步:声明response的包装类class MyResponse2 extends HttpServletResponseWrapper{ //将这个容器/a.txt,声明成员变量 private ByteArrayOutputStream srcByte; private PrintWriter out; public MyResponse2(HttpServletResponse response) { super(response); } //修改增强getWtier方法 @Override public PrintWriter getWriter() throws IOException { srcByte = new ByteArrayOutputStream(); out = new PrintWriter( new OutputStreamWriter(srcByte, "UTF-8")); return out; } //提供一个方法获取原生 的数据 public byte[] getSrc(){ if(out!=null){ out.close(); } return srcByte.toByteArray(); }}
且它的配置如下:
<filter>
<filter-name>zip2</filter-name>
<filter-class>cn.itcast.filter.GzipFilter2</filter-class>
</filter>
<filter-mapping>
<filter-name>zip2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>
同时实现对getoutputstream和getWtier压缩在myrespons2这个类中,对getoutputstream也要覆盖。
返回一个ServletOutputSteam的匿名对象。
实现witer(int b)方法,将数据写到bytearrayoyutpoutstream.
public ServletOutputStream getOutputStream() throws IOException {
srcByte = new ByteArrayOutputStream();
ServletOutputStream out = new ServletOutputStream() {
//所有IO最终都是一个个字节写出信息
@Override
public void write(int b) throws IOException {
System.err.println(">>>:"+b);
srcByte.write(b);//写到自己的缓存中去-相当于StringBuffer.append(""+b);
}
};
return out;
}
新闻热点
疑难解答