将response内响应给使用者的内容,使用GZIP压缩的方式回传给浏览器,而IE5和NS6也都有支持Gzip的压缩格式。
这个方法在之前就有人提出过,因为是让网页在输出时经过压缩,可以让传输量变小很多,虽然现在的网络频宽对于用来看网页已经绰绰有余,但是档案大小太大的网页还是会造成一定的影响。
经过Gzip压缩过的网页,档案大小可以到原本压缩前的20%。
package com.jsptw.filter;
import java.io.*;
import java.util.zip.GZIPOutputStream;
import javax.servlet.*;
import javax.servlet.http.*;
public class GZIPEncodeFilter implements Filter {
public void init(FilterConfig filterConfig) {}
public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
String transferEncoding = getGZIPEncoding((HttpServletRequest)request);
if (transferEncoding == null) {
chain.doFilter(request, response);
} else{
((HttpServletResponse)response).setHeader("Content-Encoding", transferEncoding);
GZIPEncodableResponse wrappedResponse = new GZIPEncodableResponse((HttpServletResponse)response);
chain.doFilter(request, wrappedResponse);
wrappedResponse.flush();
}
}
public void destroy() {}
private static String getGZIPEncoding(HttpServletRequest request) {
String acceptEncoding = request.getHeader("Accept-Encoding");
if (acceptEncoding == null)
return null;
acceptEncoding = acceptEncoding.toLowerCase();
if (acceptEncoding.indexOf("x-gzip") >= 0) {
return "x-gzip";
}
if (acceptEncoding.indexOf("gzip") >= 0) {
return "gzip";
}
return null;
}
private class GZIPEncodableResponse extends HttpServletResponseWrapper {
private GZIPServletStream wrappedOut;
public GZIPEncodableResponse(HttpServletResponse response) throws IOException {
super(response);
wrappedOut = new GZIPServletStream(response.getOutputStream());
}
public ServletOutputStream getOutputStream() throws IOException {
return wrappedOut;
}
private PrintWriter wrappedWriter;
public PrintWriter getWriter() throws IOException {
if (wrappedWriter == null) {
wrappedWriter = new PrintWriter( new OutputStreamWriter( getOutputStream(), getCharacterEncoding())); }
return wrappedWriter;
}
public void flush() throws IOException {
if (wrappedWriter != null) {
wrappedWriter.flush();
}
wrappedOut.finish();
}
}
private class GZIPServletStream extends ServletOutputStream {
private GZIPOutputStream outputStream;
public GZIPServletStream(OutputStream source) throws IOException {
outputStream = new GZIPOutputStream(source);
}
public void finish() throws IOException {
OutputStream.finish();
}
public void write(byte[] buf) throws IOException {
outputStream.write(buf);
}
public void write(byte[] buf, int off, int len) throws IOException {
outputStream.write(buf, off, len);
}
public void write(int c) throws IOException {
outputStream.write(c);
}
public void flush() throws IOException {
outputStream.flush();
}
public void close() throws IOException {
outputStream.close();
}
}
}
web.xml中的设定为:
<filter>
<filter-name>GZIPEncoder</filter-name>
<filter-class>com.jsptw.filter.GZIPEncodeFilter</filter-class>
</filter>
<filter-mapping>
<filter-name> GZIPEncoder</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/a_heng/archive/2009/09/22/4579979.aspx