filter攔截(Response輸出壓縮)此案例基礎知識面廣
Response兩種輸出方式:1.getOutputStream().writer();2.getWriter().writer();但不能同時存在
1.配置檔案
<filter>
<filter>
<filter-name>GzipFilter</filter-name>
<filter-class>cn.itcast.web.filter.GzipFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>*.jsp</url-pattern><!-- 注意一般不要做全棧壓縮,只壓縮html,jsp,js -->
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>GzipFilter</filter-name>
<url-pattern>*.js</url-pattern>
</filter-mapping>
2.定義GzipFilter類並 implements Filter
public class GzipFilter implements Filter {@Override
public void doFilter(ServletRequest req, ServletResponse resp,FilterChain chain) throws IOException, ServletException {
HttpServletRequest request=(HttpServletRequest) req;
HttpServletResponse response=(HttpServletResponse) resp;
MyResponse myresponse=new MyResponse(response);
chain.doFilter(request,myresponse); //response.getWriterresponse.getOutputStream
//取出緩衝資料壓縮後輸出
byte out[]=myresponse.getBuffer();
System.out.println("壓縮前:"+out.length);
byte gzipout[]=gzip(out);
System.out.println("壓縮後:"+gzipout.length);
//設定頭資訊,以壓縮方式開啟
//設定頭資訊,以壓縮方式開啟
response.setHeader("content-encoding", "gzip");
response.setHeader("content-length", gzipout.length+"");
response.getOutputStream().write(gzipout); //分別對應下面橙色A/B/C處
}
public byte[] gzip(byte b[]) throws IOException{ //壓縮
ByteArrayOutputStream bout=new ByteArrayOutputStream();
GZIPOutputStream gout=new GZIPOutputStream(bout);
gout.write(b);
gout.close();
return bout.toByteArray();
}
class MyResponse extends HttpServletResponseWrapper{ //A處對應上面response
class MyResponse extends HttpServletResponseWrapper{ //A處對應上面response
private ByteArrayOutputStream bout=new ByteArrayOutputStream(); //定義緩衝區陣列位元組流
private PrintWriter pw;
private HttpServletResponse response;
public MyResponse(HttpServletResponse response) {
super(response);
this.response=response;
}
//response輸出資料方式一:response.getOutputStream().write("xxxx");
@Override
public ServletOutputStream getOutputStream() throws IOException { //B處對應上面getOutputStream
return new MyServletOutputStream(bout); //response.getOutputStream.write(); 因ServletOutputStream是abstract類,不能直接 new
}
//response輸出資料方式二:response.getWriter().write("xxxx");
@Override
public PrintWriter getWriter() throws IOException{
//pw=new PrintWriter(bout);//字元流向位元組流寫資料,會查編碼,預設GB2312,所以要注意亂碼問題
pw=new PrintWriter(new OutputStreamWriter(bout,response.getCharacterEncoding())); //用於解決PrintWriter().writer()的亂碼問題
return pw; //MyResponse.getWriter.write("xxx");
}
//將緩衝區中的資料返回
public byte[] getBuffer(){
if(pw!=null){
pw.close(); //保證資料會寫入緩衝區,當用PrintWrier輸出時,此判斷有效
}
return bout.toByteArray();
}
}
class MyServletOutputStream extends ServletOutputStream{ //B處對應上面getOutputStream,
private ByteArrayOutputStream bout;
public MyServletOutputStream(ByteArrayOutputStream bout){
this.bout=bout;
}
@Override
public void write(int b) throws IOException { //覆寫write方法//c處對應上面write
bout.write(b);
}
}
@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
@Override
public void destroy() {
// TODO Auto-generated method stub
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data="中國";
//response.getOutputStream().write(data.getBytes("UTF-8"));//一定要指編碼,用於解決getOutputStream().write輸出亂碼
response.getWriter().write(data); //getWriter().writer輸出的亂碼問題在攔截程式碼中處理。
}
相關文章
- SAP SD基礎知識之輸出控制(Output Control)
- Shiro入門這篇就夠了【Shiro的基礎知識、回顧URL攔截】
- 【Linux基礎】壓縮和解壓Linux
- 攔截器(Interceptor)與過濾器(Filter)過濾器Filter
- Java 基礎面試知識點Java面試
- Java基礎知識面試題Java面試題
- Asp.Netcore使用Filter來實現介面的全域性異常攔截,以及前置攔截和後置攔截ASP.NETNetCoreFilter
- JS基礎知識(覆蓋JS基礎面試題)JS面試題
- Java基礎知識(牛客刷題)記錄..(持續輸出中)Java
- 基礎知識
- Java基礎面試知識點總結Java面試
- iOS | 面試知識整理 - OC基礎 (一)iOS面試
- Python 必備面試基礎知識-3Python面試
- Python 面試必備基礎知識-1Python面試
- Android面試之——數學基礎知識Android面試
- 過濾器 Filter 與 攔截器 Interceptor 的區別過濾器Filter
- 前端開發最容易出錯的基礎知識,面試常問!前端面試
- Android小知識-剖析OkHttp中的五個攔截器(中篇)AndroidHTTP
- Android小知識-剖析OkHttp中的五個攔截器(上篇)AndroidHTTP
- Android小知識-剖析OkHttp中的五個攔截器(下篇)AndroidHTTP
- 常用基礎知識面試(C++為主)面試C++
- 【Docker】Docker基礎知識(面試一籮筐)Docker面試
- Envoy基礎知識
- DockerFile基礎知識Docker
- Webpack 基礎知識Web
- js基礎知識JS
- React基礎知識React
- 程式基礎知識
- Docker基礎知識Docker
- qml基礎知識
- Mybatis基礎知識MyBatis
- python基礎知識Python
- Hadoop基礎知識Hadoop
- webpack基礎知識Web
- AI 基礎知識AI
- JSP基礎知識JS
- Dart基礎知識Dart
- RabbitMQ基礎知識MQ