import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ShowServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
PrintWriter pw = response.getWriter();
pw.write("美国美国美国美国美国美国美国美国美国美国美国美国美国美国美国<br/>");
pw.write("中国中国中国中国中国中国中国中国中国中国中国中国中国中国中国<br/>");
pw.write("法国法国法国法国法国法国法国法国法国法国法国法国法国法国法国<br/>");
//ServletOutputStream sout = response.getOutputStream();
//sout.write(data.getBytes("UTF-8"));
//MyServletOutputStream mySout = myResponse.getOutputStream();
//sout.write("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA".getBytes());
//mySout.write("AAAAAA".getBytes[]);
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
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;
import cn.itcast.web.decorator.MyResponse;
public class GzipFilter implements Filter {
public void destroy() {
}
public void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) res;
MyResponse myResponse = new MyResponse(response);
chain.doFilter(request,myResponse);
//取得缓存中的内容
byte[] data = myResponse.getBuffer();
System.out.println("压缩前:" + data.length);
//进行压缩
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(bout);
gout.write(data);
gout.flush();
gout.close();
data = bout.toByteArray();
System.out.println("压缩后:" + data.length);
//通知浏览器接收是的一个压缩型数据库和长度
response.setHeader("content-encoding","gzip");
response.setHeader("content-length",data.length+"");
//以字节方式真正输出到浏览器
response.getOutputStream().write(data);
//无限压缩,无出口,类似环无限循环
//myResponse.getOutputStream().write(data);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
}
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
//对response对象的装饰/包装
public class MyResponse extends HttpServletResponseWrapper{
private HttpServletResponse response;
//缓存
private ByteArrayOutputStream bout = new ByteArrayOutputStream();
private PrintWriter pw;
public MyResponse(HttpServletResponse response) {
super(response);
this.response = response;
}
//重写父类方法,目的是将字节输出到缓存中去[字节]
public ServletOutputStream getOutputStream() throws IOException {
return new MyServletOutputStream(bout);
}
//重写父类方法,目的是将字符输出到缓存中去[字符]
public PrintWriter getWriter() throws IOException {
pw = new PrintWriter(new OutputStreamWriter(bout,"UTF-8"));
return pw;
}
//取得缓存中的数据
public byte[] getBuffer(){
if(pw!=null){
pw.flush();
}
return bout.toByteArray();
}
}
//带有缓存功能ServletOutputStream
class MyServletOutputStream extends ServletOutputStream{
private ByteArrayOutputStream bout;
public MyServletOutputStream(ByteArrayOutputStream bout) {
this.bout = bout;
}
public void write(int b) throws IOException {
}
public void write(byte[] bytes) throws IOException {
//将字节数组的内容写入缓存
bout.write(bytes);
//确保所有字节数组内容进入缓存
bout.flush();
}
}
原文地址:http://blog.51cto.com/357712148/2105519