不知道该过滤器(三个java程序,不大)的具体用途和必要性,谁给我讲讲.

四十岁开始写程序 2005-05-27 04:48:25
package com.terac.filters;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// Referenced classes of package com.terac.filters:
// GZIPResponseWrapper

public class GZIPFilter
implements Filter
{

public GZIPFilter()
{
}

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
throws IOException, ServletException
{
if(req instanceof HttpServletRequest)
{
HttpServletRequest request = (HttpServletRequest)req;
HttpServletResponse response = (HttpServletResponse)res;
String ae = request.getHeader("accept-encoding");
if(ae != null && ae.indexOf("gzip") != -1)
{
GZIPResponseWrapper gZIPResponseWrapper = new GZIPResponseWrapper(response);
chain.doFilter(req, gZIPResponseWrapper);
gZIPResponseWrapper.finishResponse();
return;
}
chain.doFilter(req, res);
}
}

public void init(FilterConfig filterconfig)
{
}

public void destroy()
{
}
}
////////----------------------

package com.terac.filters;

import java.io.*;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;

// Referenced classes of package com.terac.filters:
// GZIPResponseStream

public class GZIPResponseWrapper extends HttpServletResponseWrapper
{

public GZIPResponseWrapper(HttpServletResponse response)
{
super(response);
origResponse = null;
stream = null;
writer = null;
origResponse = response;
}

public ServletOutputStream createOutputStream()
throws IOException
{
return new GZIPResponseStream(origResponse);
}

public void finishResponse()
{
try
{
if(writer != null)
writer.close();
else
if(stream != null)
stream.close();
}
catch(IOException e) { }
}

public void flushBuffer()
throws IOException
{
stream.flush();
}

public ServletOutputStream getOutputStream()
throws IOException
{
if(writer != null)
throw new IllegalStateException("getWriter() has already been called!");
if(stream == null)
stream = createOutputStream();
return stream;
}

public PrintWriter getWriter()
throws IOException
{
if(writer != null)
return writer;
if(stream != null)
{
throw new IllegalStateException("getOutputStream() has already been called!");
}
else
{
stream = createOutputStream();
writer = new PrintWriter(new OutputStreamWriter(stream, "UTF-8"));
return writer;
}
}

public void setContentLength(int i)
{
}

protected HttpServletResponse origResponse;
protected ServletOutputStream stream;
protected PrintWriter writer;
}

//--------------------------

package com.terac.filters;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

public class GZIPResponseStream extends ServletOutputStream
{


protected ByteArrayOutputStream baos;
protected GZIPOutputStream gzipstream;
protected boolean closed;
protected HttpServletResponse response;
protected ServletOutputStream output;


public GZIPResponseStream(HttpServletResponse response)
throws IOException
{
baos = null;
gzipstream = null;
closed = false;
this.response = null;
output = null;

closed = false;
this.response = response;
output = response.getOutputStream();
baos = new ByteArrayOutputStream();
gzipstream = new GZIPOutputStream(baos);
}

public void close()
throws IOException
{
if(closed)
{
throw new IOException("This output stream has already been closed");
} else
{
gzipstream.finish();
byte bytes[] = baos.toByteArray();
response.addHeader("Content-Length", Integer.toString(bytes.length));
response.addHeader("Content-Encoding", "gzip");
output.write(bytes);
output.flush();
output.close();
closed = true;
return;
}
}

public void flush()
throws IOException
{
if(closed)
{
throw new IOException("Cannot flush a closed output stream");
} else
{
gzipstream.flush();
return;
}
}

public void write(int b)
throws IOException
{
if(closed)
{
throw new IOException("Cannot write to a closed output stream");
} else
{
gzipstream.write((byte)b);
return;
}
}

public void write(byte b[])
throws IOException
{
write(b, 0, b.length);
}

public void write(byte b[], int off, int len)
throws IOException
{
if(closed)
{
throw new IOException("Cannot write to a closed output stream");
} else
{
gzipstream.write(b, off, len);
return;
}
}

public boolean closed()
{
return closed;
}

public void reset()
{
}


}


...全文
107 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
peihexian 2005-05-31
  • 打赏
  • 举报
回复
知道答案了就结帖啊?
Vincent2013SH 2005-05-30
  • 打赏
  • 举报
回复
mark
peihexian 2005-05-30
  • 打赏
  • 举报
回复
楼主应该了解一下http通讯协议的知识,在http通讯中,如我们使用IE浏览器去访问某一个网站时,有一种压缩传输数据流的方式,叫GZIP方式,这个filter程序就是检测一下客户端浏览器是否支持GZIP压缩传输方式,如果支持的话就把由服务器端发给客户端的数据流压缩一下。
pigo 2005-05-28
  • 打赏
  • 举报
回复


用来进行gzip压缩传输。
具体请google,也可以看:

http://zolazhou.blogchina.com/401926.html

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧