代码来之哪里,不记得了
GzipFilter
public class GzipFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器被执行了");
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//1.对response进行改进
MyResponse resp = new MyResponse((HttpServletResponse) response);
//2.放行,让后台做response做输出操作
chain.doFilter(request, resp);
//3.从我们定义的“字节数组输出流”中取出数据进行压缩
ByteArrayOutputStream baout = resp.getBaout();
byte bs[] = baout.toByteArray();// 源字节数据
System.out.println("压缩前大小:" + bs.length);
ByteArrayOutputStream zipBaout = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(zipBaout);
gout.write(bs);// 把数据压缩到baout中
gout.close();
bs = zipBaout.toByteArray();//得到压缩后的字节
System.out.println("压缩后大小:" + bs.length);
//4.再用原生的reponse输出压缩后的内容
HttpServletResponse httpResp = (HttpServletResponse) response;
// 输出之前告诉客户端:我们的数据是gzip格式,然后输出
httpResp.setHeader("Content-Encoding", "gzip");
httpResp.setContentLength(bs.length);
OutputStream out = httpResp.getOutputStream();
out.write(bs);
}
@Override
public void destroy() {
}
}
class MyResponse extends HttpServletResponseWrapper { //HttpServletResponse实现类
private PrintWriter pw;
private ByteArrayOutputStream baout = new ByteArrayOutputStream();
/*
* 字节数组输了流(带缓存功能):所有发送到输出流的数据保存在该字节数组缓冲区中,可以转成字节数组
* 在此的作用:为了将response输出的内容先存到字节数组缓冲区中,以便后续的压缩处理*/
public MyResponse(HttpServletResponse response) {
super(response);
}
//重新实现response的该方法
public ServletOutputStream getOutputStream() throws IOException {
return new MyOutputStream(baout);
}
public ByteArrayOutputStream getBaout() {
if(pw!=null){
pw.flush();
//这里很重要,如果不flush或close,不把字符流刷出去,baout中是不会有数据的.
}
return baout;
}
//重新实现response的该方法
public PrintWriter getWriter() throws IOException {
//字节流转字符流
pw = new PrintWriter(new OutputStreamWriter(baout, "utf-8"),true);
return pw;
}
}
/**
* 自定义ServletOutputStream子类
* 目的:保证response获取到的outputStream是我们定义的“字节数组输入流”
* ,且调用write方法时,是往“字节数组输入流”的缓存区中写入*/
class MyOutputStream extends ServletOutputStream {
private ByteArrayOutputStream baout = null;
public MyOutputStream(ByteArrayOutputStream baout) {
this.baout = baout;
}
@Override
public void write(int b) throws IOException {
baout.write(b);//把指定内容写入到 字节数组输入流
}
}
测试servlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// String str = "go go go let's go";
// //1\获取字节数组
// byte[] bytes = str.getBytes() ;
//
// System.out.println("压缩前的长度:" + bytes.length);
// //2\
// ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
// GZIPOutputStream gzip = new GZIPOutputStream(baos) ;
//
// gzip.write(bytes) ;
// gzip.close() ;
// //3\
// bytes = baos.toByteArray() ;
// System.out.println("压缩后的长度:" + bytes.length);
response.setContentType("text/html;charset=utf-8");
String str = "And I memorize each word我记住的每个字眼";
OutputStream out = response.getOutputStream();
out.write(str.getBytes("utf-8"));
//response.getWriter().write(str);
}