本系列文章導讀:
設置站點黑名單的過濾器(BannedAccessFilter)
將響應數據進行壓縮處理的過濾器(CompressionFilter)
替換禁用語(指定關鍵字)的過濾器(StopWordsFilter)
功能描述
如果瀏覽器支持 gzip 壓縮格式的數據,則將響應的數據使用 gzip 壓縮后再輸出。
使用方法
在 java web 項目的 web.xml 文件中添加如下代碼。
<!--壓縮過濾器的配置 開始 --> <filter> <filter-name>CompressionFilter</filter-name> <filter-class>com.hmw.filter.CompressionFilter</filter-class> </filter> <filter-mapping> <filter-name>CompressionFilter</filter-name> <servlet-name>/LongServlet</servlet-name> </filter-mapping> <!--壓縮過濾器的配置 結束 -->
過濾器源碼
CompressionFilter.java
package com.hmw.filter; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.util.zip.GZIPOutputStream; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * 壓縮過濾器
* 如果瀏覽器支持 gzip 壓縮格式的數據,則將響應的數據使用 gzip 壓縮后再輸出。 * * @author 何明旺 */ public class CompressionFilter implements Filter { @Override public void init(FilterConfig config) throws ServletException { } /** * 如果瀏覽器不支持 gzip 壓縮,則不做直接放行(不做壓縮處理)
* 反之,將HTTP響應頭的編碼設置為gzip,然后將響應數據使用 gzip 進行壓縮處理。 */ @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse res = (HttpServletResponse) response; if (!isGzipSupported(req)) { // Invoke resource normally. chain.doFilter(req, res); return; } // 將響應頭信息中的內容編碼設置為 gzip res.setHeader("Content-Encoding", "gzip"); // 調用資源,使用 CharArrayWrapper 包裝輸出 CharArrayWrapper responseWrapper = new CharArrayWrapper(res); chain.doFilter(req, responseWrapper); // 取得存放輸出數據的 char 型數組 char[] responseChars = responseWrapper.toCharArray(); // 將響應數據壓縮后存入一個 byte 型的數組,然后輸出到 ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); GZIPOutputStream zipOut = new GZIPOutputStream(byteStream); OutputStreamWriter tempOut = new OutputStreamWriter(zipOut); // 將原來的響應數據壓縮后寫入二字節輸出流 tempOut.write(responseChars); // 關閉輸出流 tempOut.close(); // 更新響應頭信息中 Content-Length 的值。 res.setContentLength(byteStream.size()); // 將壓縮后的數據發送至客戶端 OutputStream realOut = res.getOutputStream(); byteStream.writeTo(realOut); } @Override public void destroy() { } /** * 檢測瀏覽器是否支持 Gzip 壓縮 * * @param req HTTP 請求對象 * @return 如果瀏覽器支持 Gzip 壓縮,則返回 true,反之,則返回 false */ private boolean isGzipSupported(HttpServletRequest req) { String browserEncodings = req.getHeader("Accept-Encoding"); return ((browserEncodings != null) && (browserEncodings.indexOf("gzip") != -1)); } }
CharArrayWrapper.java
package com.hmw.filter;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
/**
* A response wrapper that takes everything the client would normally output and
* saves it in one big character array.
*/
public class CharArrayWrapper extends HttpServletResponseWrapper {
private CharArrayWriter charWriter;
/**
* Initializes wrapper.
* <P>
* First, this constructor calls the parent constructor. That call is
* crucial so that the response is stored and thus setHeader, *setStatus,
* addCookie, and so forth work normally.
* <P>
* Second, this constructor creates a CharArrayWriter that will be used to
* accumulate the response.
*/
public CharArrayWrapper(HttpServletResponse response) {
super(response);
charWriter = new CharArrayWriter();
}
/**
* When servlets or JSP pages ask for the Writer, don't give them the real
* one. Instead, give them a version that writes into the character array.
* The filter needs to send the contents of the array to the client (perhaps
* after modifying it).
*/
@Override
public PrintWriter getWriter() {
return new PrintWriter(charWriter);
}
/**
* Get a String representation of the entire buffer.
* <P>
* Be sure <B>not</B> to call this method multiple times on the same
* wrapper. The API for CharArrayWriter does not guarantee that it
* "remembers" the previous value, so the call is likely to make a new
* String every time.
*/
@Override
public String toString() {
return charWriter.toString();
}
/** Get the underlying character array. */
public char[] toCharArray() {
return charWriter.toCharArray();
}
}
