过滤器-->GZIP压缩
1.创建一个 可以使用GZIPOutputStream 压缩的流 package com.zh.yasuo2; import java.io.IOException; import java.util.zip.GZIPOutputStream; import javax.servlet.ServletOutputStream; public class GzipServletOupputStream extends ServletOutputStream{ private GZIPOutputStream gzipOutputStream; @Override public void write(int b) throws IOException { // TODO Auto-generated method stub System.out.println("b="+b); gzipOutputStream.write(b); } public GzipServletOupputStream(ServletOutputStream servletOutPutStream) throws IOException{ this.gzipOutputStream=new GZIPOutputStream(servletOutPutStream); } public GZIPOutputStream getGZIPOutputStream(){ return gzipOutputStream; } } 2. 继承 HttpServletResponseWrapper 重写 getOutputStream(), getWriter()方法. 使其流是可以压缩的 package com.zh.yasuo2; import java.io.*; import java.util.zip.GZIPOutputStream; import javax.servlet.*; import javax.servlet.http.*; public class GzipWrapper extends HttpServletResponseWrapper{ private GzipServletOupputStream gzipServletOupputStream; private PrintWriter printwrite; public GzipWrapper(HttpServletResponse response) { super(response); // TODO Auto-generated constructor stub } //重写 getOutputStream()方法 public ServletOutputStream getOutputStream()throws IOException{ if(printwrite != null){ throw new IllegalStateException(); } if(gzipServletOupputStream ==null){ gzipServletOupputStream=new GzipServletOupputStream(this.getResponse().getOutputStream()); } return gzipServletOupputStream; } //重写getWriter()方法 public PrintWriter getWriter() throws IOException{ if(gzipServletOupputStream != null){ throw new IllegalStateException(); } if(printwrite== null){ gzipServletOupputStream=new GzipServletOupputStream(this.getResponse().getOutputStream()); OutputStreamWriter osw=new OutputStreamWriter(gzipServletOupputStream,getResponse().getCharacterEncoding()); printwrite=new PrintWriter(osw); //System.out.println("printwrite"+printwrite.toString()); } return printwrite; } public void setContentLength(int len){ } public GZIPOutputStream getGZIPOutputStream(){ if(this.gzipServletOupputStream == null){ return null; } return this.gzipServletOupputStream.getGZIPOutputStream(); } } 3.使用过滤器 package com.zh.yasuo2; import java.io.IOException; import java.util.zip.GZIPOutputStream; import javax.servlet.*; import javax.servlet.http.*; public class GzipFilter implements Filter{ public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { // TODO Auto-generated method stub HttpServletRequest req=(HttpServletRequest) arg0; HttpServletResponse res=(HttpServletResponse) arg1; //用encodings 来判断浏览器是否有 gzip压缩的功能 String encodings=req.getHeader("accept-encoding"); if((encodings!=null) && (encodings.indexOf("gzip"))>-1){ GzipWrapper gzipWrapper=new GzipWrapper(res); //设置响应内容编码为gzip格式 gzipWrapper.setHeader("content-encoding", "gzip"); arg2.doFilter(arg0, gzipWrapper); GZIPOutputStream gzipOutputStream=gzipWrapper.getGZIPOutputStream(); if(gzipOutputStream !=null ){ gzipOutputStream.finish(); //写成压缩输出 System.out.println("finish();"); } }else{ arg2.doFilter(arg0, arg1); } } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } } 4.web.xml <filter> <filter-name>GzipFilter</filter-name> <filter-class>com.zh.yasuo2.GzipFilter</filter-class> </filter> <filter-mapping> <filter-name>GzipFilter</filter-name> <url-pattern>/GzipServler</url-pattern> </filter-mapping> 5. servlet 实验. package com.zh.yasuo2; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class GzipServler extends HttpServlet { /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the GET method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out .println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">"); out.println("<HTML>"); out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>"); out.println(" <BODY>"); out.print(" This is "); out.print(this.getClass()); out.println(", using the POST method"); out.println(" </BODY>"); out.println("</HTML>"); out.flush(); out.close(); } }