gzip 執行個體應用

來源:互聯網
上載者:User
本文章摘編、轉載需要註明來源 http://write.blog.csdn.net/postedit/8575066

web頁面的css跟js傳輸是影響頁面載入速度的重要因素,所以一般我們都會採用gzip流來壓縮傳輸,載入速度明顯提升一個數量級,而且現在這gzip是基本的主流瀏覽器都會支援

先來看下我們的兩個輔助類

/** *  * @author shadow * @email 124010356@qq.com * @create 2012.04.28 */public class GzipResponse extends HttpServletResponseWrapper {private GzipStream stream;private PrintWriter writer;protected int len;public GzipResponse(HttpServletResponse response) throws IOException {super(response);stream = new GzipStream(response.getOutputStream());}public GzipResponse(HttpServletResponse response, GzipStream stream) throws IOException{super(response);this.stream = stream;}public void setContentLength(int len) {this.len = len;}@Overridepublic ServletOutputStream getOutputStream() throws IOException {return stream;}public GzipStream getStream() {return stream;}public void setStream(GzipStream stream) {this.stream = stream;}@Overridepublic PrintWriter getWriter() throws IOException {if (writer == null) {writer = new PrintWriter(new OutputStreamWriter(getOutputStream(),getCharacterEncoding()));}return writer;}public void flush() throws IOException {if (writer != null) {writer.flush();}stream.finish();}}

/** *  * @author shadow * @email 124010356@qq.com * @create 2012.04.28 */public class GzipStream extends ServletOutputStream {private GZIPOutputStream zipStream;public GzipStream(OutputStream out) throws IOException {zipStream = new GZIPOutputStream(out);}@Overridepublic void flush() throws IOException {zipStream.flush();}@Overridepublic void write(byte[] b, int off, int len) throws IOException {zipStream.write(b, off, len);}@Overridepublic void write(byte[] b) throws IOException {zipStream.write(b);}@Overridepublic void write(int arg0) throws IOException {zipStream.write(arg0);}public void finish() throws IOException {zipStream.finish();}public void close() throws IOException {zipStream.close();}}

然後我們就開始寫個filter

/** * GZIP流壓縮檔傳輸 壓縮率50%-80% *  * @author shadow * @email 124010356@qq.com * @create 2012.04.28 */public class GzipFilter implements Filter {private final String ENABLE = "enable";private final String GZIP = "gzip";private boolean enable = true;public void init(FilterConfig config) throws ServletException {// 擷取配置參數String enable = config.getInitParameter(ENABLE);if (null == enable)// 預設是開啟壓縮功能this.enable = true;elsethis.enable = Boolean.valueOf(enable);}public void doFilter(ServletRequest rq, ServletResponse rp,FilterChain chain) throws IOException, ServletException {HttpServletRequest request = (HttpServletRequest) rq;HttpServletResponse response = (HttpServletResponse) rp;// 瀏覽器支援GZIP以及開啟GZIP功能的情況if (isGZipEncoding(request) && enable) {GzipResponse zipResponse = new GzipResponse(response);response.setHeader("Content-Encoding", GZIP);chain.doFilter(request, zipResponse);zipResponse.flush();} else {chain.doFilter(request, response);}}/** * 判斷瀏覽器是否支援GZIP *  * @param request * @return Boolean */private boolean isGZipEncoding(HttpServletRequest request) {boolean flag = false;String encoding = request.getHeader("Accept-Encoding");if (encoding.indexOf(GZIP) != -1)flag = true;return flag;}public void destroy() {}}

最後我們來配置web.xml

<!-- gzip filter --><filter><filter-name>gzipFilter</filter-name><filter-class>com.shadow.extras.gzip.GzipFilter</filter-class><init-param><param-name>enable</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>gzipFilter</filter-name><url-pattern>*.js</url-pattern><url-pattern>*.css</url-pattern><url-pattern>*.jsp</url-pattern><url-pattern>*.html</url-pattern></filter-mapping>

大功告成,客戶請求頁面時候css,js,jsp,html檔案的時候會被攔截然後使用gzip壓縮傳輸到用戶端

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.