?? compressionservletresponsewrapper.java
字號(hào):
package compressionFilters;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Locale;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.ServletResponse;
import javax.servlet.ServletResponseWrapper;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
public class CompressionServletResponseWrapper
extends HttpServletResponseWrapper {
protected HttpServletResponse origResponse = null;
protected static final String info = "CompressionServletResponseWrapper";
protected ServletOutputStream stream = null;
protected PrintWriter writer = null;
protected int threshold = 0;
protected String contentType = null;
public CompressionServletResponseWrapper(HttpServletResponse response) {
super(response);
origResponse = response;
}
public void setContentType(String contentType) {
this.contentType = contentType;
origResponse.setContentType(contentType);
}
//設(shè)置是否啟用壓縮的臨界值
public void setCompressionThreshold(int threshold) {
this.threshold = threshold;
}
//創(chuàng)建這個(gè)自定義response對(duì)象包含的輸出流對(duì)象
public ServletOutputStream createOutputStream() throws IOException {
CompressionResponseStream stream =
new CompressionResponseStream(origResponse);
stream.setBuffer(threshold);
return stream;
}
/* 為了防止目標(biāo)Servlet沒有關(guān)閉輸出流對(duì)象,F(xiàn)ilter程序必須調(diào)用這個(gè)方法,
* 以便自動(dòng)幫助擁護(hù)關(guān)閉輸出流對(duì)象*/
public void finishResponse() {
try {
if (writer != null) {
writer.close();
} else {
if (stream != null)
stream.close();
}
} catch (IOException e) {
}
}
//覆蓋flushBuffer方法
public void flushBuffer() throws IOException {
((CompressionResponseStream)stream).flush();
}
//覆蓋getOutputStream方法
public ServletOutputStream getOutputStream() throws IOException {
if (writer != null)
throw new IllegalStateException(
"getWriter() has already been called for this response");
if (stream == null)
stream = createOutputStream();
return (stream);
}
//覆蓋getWriter方法
public PrintWriter getWriter() throws IOException {
if (writer != null)
return (writer);
if (stream != null)
throw new IllegalStateException(
"getOutputStream() has already been called for this response");
stream = createOutputStream();
//String charset = getCharsetFromContentType(contentType);
String charEnc = origResponse.getCharacterEncoding();
if (charEnc != null) {
writer = new PrintWriter(
new OutputStreamWriter(stream, charEnc));
} else {
writer = new PrintWriter(stream);
}
return (writer);
}
//最終的數(shù)據(jù)要被壓縮,外界調(diào)用setContentLength方法不應(yīng)該起作用
public void setContentLength(int length) {
}
//從Content-Type頭字段中提取當(dāng)前的字符集編碼信息
private static String getCharsetFromContentType(String type) {
if (type == null) {
return null;
}
int semi = type.indexOf(";");
if (semi == -1) {
return null;
}
String afterSemi = type.substring(semi + 1);
int charsetLocation = afterSemi.indexOf("charset=");
if(charsetLocation == -1) {
return null;
} else {
String afterCharset = afterSemi.substring(charsetLocation + 8);
String encoding = afterCharset.trim();
return encoding;
}
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -