本文章摘編、轉載需要註明來源 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壓縮傳輸到用戶端