04 過濾器案例之-壓縮過濾器

代碼來之哪里,不記得了

Paste_Image.png

GzipFilter

public class GzipFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("過濾器被執(zhí)行了");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response,
            FilterChain chain) throws IOException, ServletException {

        //1.對response進行改進
        MyResponse resp = new MyResponse((HttpServletResponse) response);       

        //2.放行,讓后臺做response做輸出操作
        chain.doFilter(request, resp);

        //3.從我們定義的“字節(jié)數(shù)組輸出流”中取出數(shù)據(jù)進行壓縮
        ByteArrayOutputStream baout = resp.getBaout();
        byte bs[] = baout.toByteArray();// 源字節(jié)數(shù)據(jù)
        System.out.println("壓縮前大小:" + bs.length);
        
        ByteArrayOutputStream zipBaout = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(zipBaout);
        gout.write(bs);// 把數(shù)據(jù)壓縮到baout中
        gout.close();
        
        bs = zipBaout.toByteArray();//得到壓縮后的字節(jié)
        System.out.println("壓縮后大小:" + bs.length);
        
        //4.再用原生的reponse輸出壓縮后的內(nèi)容
        HttpServletResponse httpResp = (HttpServletResponse) response;
        // 輸出之前告訴客戶端:我們的數(shù)據(jù)是gzip格式,然后輸出
        httpResp.setHeader("Content-Encoding", "gzip");
        httpResp.setContentLength(bs.length);
        OutputStream out = httpResp.getOutputStream();
        out.write(bs);
        
    }

    @Override
    public void destroy() {
        
    }

}

class MyResponse extends HttpServletResponseWrapper { //HttpServletResponse實現(xiàn)類
    private PrintWriter pw;
    private ByteArrayOutputStream baout = new ByteArrayOutputStream();
    /*
     * 字節(jié)數(shù)組輸了流(帶緩存功能):所有發(fā)送到輸出流的數(shù)據(jù)保存在該字節(jié)數(shù)組緩沖區(qū)中,可以轉(zhuǎn)成字節(jié)數(shù)組
     * 在此的作用:為了將response輸出的內(nèi)容先存到字節(jié)數(shù)組緩沖區(qū)中,以便后續(xù)的壓縮處理*/
    
    public MyResponse(HttpServletResponse response) {
        super(response);
    }

    //重新實現(xiàn)response的該方法
    public ServletOutputStream getOutputStream() throws IOException {
        return new MyOutputStream(baout);
    }

    public ByteArrayOutputStream getBaout() {
        if(pw!=null){
            pw.flush();
            //這里很重要,如果不flush或close,不把字符流刷出去,baout中是不會有數(shù)據(jù)的.
        }
        return baout;
    }
    
    //重新實現(xiàn)response的該方法
    public PrintWriter getWriter() throws IOException {
        //字節(jié)流轉(zhuǎn)字符流
        pw = new PrintWriter(new OutputStreamWriter(baout, "utf-8"),true);
        return pw;
    }

}

/**
 * 自定義ServletOutputStream子類
 * 目的:保證response獲取到的outputStream是我們定義的“字節(jié)數(shù)組輸入流”
 * ,且調(diào)用write方法時,是往“字節(jié)數(shù)組輸入流”的緩存區(qū)中寫入*/
class MyOutputStream extends ServletOutputStream {
    private ByteArrayOutputStream baout = null;

    public MyOutputStream(ByteArrayOutputStream baout) {
        this.baout = baout;
    }

    @Override
    public void write(int b) throws IOException {
        baout.write(b);//把指定內(nèi)容寫入到 字節(jié)數(shù)組輸入流
    }
}

測試servlet

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

//      String str = "go go go let's go";
//      //1\獲取字節(jié)數(shù)組
//      byte[] bytes = str.getBytes() ;
//      
//      System.out.println("壓縮前的長度:" + bytes.length); 
//      //2\
//      ByteArrayOutputStream baos = new ByteArrayOutputStream() ; 
//      GZIPOutputStream  gzip = new GZIPOutputStream(baos) ;
//      
//      gzip.write(bytes) ; 
//      gzip.close() ;
//      //3\
//      bytes = baos.toByteArray() ;
//      System.out.println("壓縮后的長度:" + bytes.length);
        

    
        response.setContentType("text/html;charset=utf-8");
        String str = "And I memorize each word我記住的每個字眼";
        
        OutputStream out = response.getOutputStream();
        out.write(str.getBytes("utf-8"));
        
        //response.getWriter().write(str);
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 1. Java基礎(chǔ)部分 基礎(chǔ)部分的順序:基本語法,類相關(guān)的語法,內(nèi)部類的語法,繼承相關(guān)的語法,異常的語法,線程的語...
    子非魚_t_閱讀 31,754評論 18 399
  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,869評論 18 139
  • 轉(zhuǎn)自陳明乾的博客,可能有一定更新。 轉(zhuǎn)原文聲明:原創(chuàng)作品,允許轉(zhuǎn)載,轉(zhuǎn)載時請務(wù)必以超鏈接形式標(biāo)明文章 原始出處 、...
    C86guli閱讀 4,716評論 6 72
  • 從三月份找實習(xí)到現(xiàn)在,面了一些公司,掛了不少,但最終還是拿到小米、百度、阿里、京東、新浪、CVTE、樂視家的研發(fā)崗...
    時芥藍閱讀 42,356評論 11 349
  • -1- 作為一個女孩子,從小就被告誡自己需要溫柔懂事,大氣而又不失本分,要和同學(xué)好好相處,要記得收斂自己的鋒芒…身...
    路不搖閱讀 281評論 0 2