過濾器壓縮全站jsp

來源:互聯網
上載者:User

標籤:des   style   blog   http   io   ar   color   os   使用   

過濾器:

 1 public class Gzip implements Filter { 2  3     public void destroy() {} 4  5     public void doFilter(ServletRequest request, ServletResponse response, 6             FilterChain chain) throws IOException, ServletException { 7         HttpServletResponse resp=(HttpServletResponse) response; 8         MyResp myResp=new MyResp(resp);//封裝過的response 9         chain.doFilter(request, myResp);//允許存取10         11         byte[] srcBytes= myResp.getSrcByte();//擷取來源資料12         ByteArrayOutputStream bOutStream=new ByteArrayOutputStream();//目標容器13         GZIPOutputStream zipOutStream=new GZIPOutputStream(bOutStream);//壓縮公用程式14         zipOutStream.write(srcBytes);//寫到容器中15         zipOutStream.close();//如果不關閉,會在緩衝中,不寫出16         17         byte[] descByte=bOutStream.toByteArray();18         19         System.out.println("-------------------");20         System.out.println("壓縮前:"+srcBytes.length);21         System.out.println("壓縮後:"+ descByte.length);22         System.out.println("-------------------");23         24         //使用原生response輸出25         resp.setContentType("text/html;charset=utf-8");26         resp.setHeader("Content-Encoding", "Gzip");27         resp.setContentLength(srcBytes.length);28         29         //輸出30         OutputStream out=resp.getOutputStream();31         out.write(descByte);//關閉32         33         34     }35     public void init(FilterConfig filterConfig) throws ServletException {}36 37 }38 39 class MyResp extends HttpServletResponseWrapper40 {41     private  PrintWriter outWrite;42     private ByteArrayOutputStream srcByte;43     //擷取原生資料44     public byte[] getSrcByte() {45         if(outWrite!=null)//如果不關閉流,不會顯示,因為資料在緩衝中,沒有寫出46         {47             outWrite.close();48         }49         return srcByte.toByteArray();50     }51 52     public MyResp(HttpServletResponse response) {53         super(response);54 55     }56 57     //封裝getWrite()方法,jsp頁面輸出內容使用的是response.getWriter().write("")58     //所以要封裝59     @Override60     public PrintWriter getWriter() throws IOException {61         srcByte=new ByteArrayOutputStream();//輸出的來源資料62         outWrite=new PrintWriter(new OutputStreamWriter(srcByte,"utf-8"));        63         return outWrite;64     }65     66 }

如果過濾器需要過濾Servlet,則需要用response.getWriter().write("")輸出,否則不會被過濾

1 response.setContentType("text/html;charset=utf-8");    2         String str="你好,同學;Hello World";3         PrintWriter writer=response.getWriter();4         writer.write(str);5         writer.close();

如果Servlet中使用response.getOutputStream().write("")輸出,則需要封裝getOutputStream()方法

1 response.setContentType("text/html;charset=utf-8");    2         String str="你好,同學;Hello World";3         String times=request.getParameter("times");4         int n=Integer.valueOf(times);5         OutputStream out=response.getOutputStream();6         for (int i = 0; i < n; i++) {7             out.write(str.getBytes("utf-8"));8         }9         out.close();

封裝getOutputStream()方法

 1     @Override 2     public ServletOutputStream getOutputStream() throws IOException { 3         srcByte =new ByteArrayOutputStream();//輸出的來源資料 4         ServletOutputStream out=new ServletOutputStream() {             5             @Override 6             public void write(int b) throws IOException { 7                 srcByte.write(b);                 8             } 9         };10         return out;11     }

 

過濾器壓縮全站jsp

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.