Skip to content

Commit bd9527e

Browse files
committed
全站压缩
1 parent 3e7deee commit bd9527e

23 files changed

+411
-0
lines changed

myGzipWeb/.classpath

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="con" path="melibrary.com.genuitec.eclipse.j2eedt.core.MYECLIPSE_JAVAEE_6_CONTAINER"/>
6+
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
7+
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
8+
<classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
9+
</classpath>

myGzipWeb/.mymetadata

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-module
3+
type="WEB"
4+
name="myGzipWeb"
5+
id="myeclipse.1471575965748"
6+
context-root="/myGzipWeb"
7+
j2ee-spec="6.0"
8+
archive="myGzipWeb.war">
9+
<attributes>
10+
<attribute name="webrootdir" value="WebRoot" />
11+
</attributes>
12+
</project-module>
13+

myGzipWeb/.project

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>myGzipWeb</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.wst.common.project.facet.core.builder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.wst.jsdt.core.javascriptValidator</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
<buildCommand>
19+
<name>com.genuitec.eclipse.j2eedt.core.WebClasspathBuilder</name>
20+
<arguments>
21+
</arguments>
22+
</buildCommand>
23+
<buildCommand>
24+
<name>org.eclipse.jdt.core.javabuilder</name>
25+
<arguments>
26+
</arguments>
27+
</buildCommand>
28+
<buildCommand>
29+
<name>com.genuitec.eclipse.j2eedt.core.J2EEProjectValidator</name>
30+
<arguments>
31+
</arguments>
32+
</buildCommand>
33+
<buildCommand>
34+
<name>com.genuitec.eclipse.j2eedt.core.DeploymentDescriptorValidator</name>
35+
<arguments>
36+
</arguments>
37+
</buildCommand>
38+
<buildCommand>
39+
<name>org.eclipse.wst.validation.validationbuilder</name>
40+
<arguments>
41+
</arguments>
42+
</buildCommand>
43+
<buildCommand>
44+
<name>com.genuitec.eclipse.ast.deploy.core.DeploymentBuilder</name>
45+
<arguments>
46+
</arguments>
47+
</buildCommand>
48+
</buildSpec>
49+
<natures>
50+
<nature>com.genuitec.eclipse.ast.deploy.core.deploymentnature</nature>
51+
<nature>org.eclipse.jem.workbench.JavaEMFNature</nature>
52+
<nature>com.genuitec.eclipse.j2eedt.core.webnature</nature>
53+
<nature>org.eclipse.jdt.core.javanature</nature>
54+
<nature>org.eclipse.wst.jsdt.core.jsNature</nature>
55+
<nature>org.eclipse.wst.common.project.facet.core.nature</nature>
56+
<nature>org.eclipse.wst.common.modulecore.ModuleCoreNature</nature>
57+
</natures>
58+
</projectDescription>

myGzipWeb/.settings/.jsdtscope

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="WebRoot"/>
4+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.WebProject">
6+
<attributes>
7+
<attribute name="hide" value="true"/>
8+
</attributes>
9+
</classpathentry>
10+
<classpathentry kind="con" path="org.eclipse.wst.jsdt.launching.baseBrowserLibrary"/>
11+
<classpathentry kind="output" path=""/>
12+
</classpath>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#Fri Aug 19 11:06:07 CST 2016
2+
eclipse.preferences.version=1
3+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
4+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
5+
org.eclipse.jdt.core.compiler.compliance=1.6
6+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
7+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
8+
org.eclipse.jdt.core.compiler.source=1.6
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project-modules id="moduleCoreId" project-version="1.5.0">
3+
<wb-module deploy-name="myGzipWeb">
4+
<wb-resource deploy-path="/" source-path="/WebRoot"/>
5+
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
6+
<property name="context-root" value="/myGzipWeb"/>
7+
<property name="java-output-path" value="/myGzipWeb/WebRoot/WEB-INF/classes"/>
8+
</wb-module>
9+
</project-modules>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<faceted-project>
3+
<fixed facet="wst.jsdt.web"/>
4+
<installed facet="java" version="1.6"/>
5+
<installed facet="jst.web" version="3.0"/>
6+
<installed facet="wst.jsdt.web" version="1.0"/>
7+
</faceted-project>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.eclipse.wst.jsdt.launching.baseBrowserLibrary
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Window

myGzipWeb/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
全站压缩
2+
3+
全站压缩,最大的好久就是帮客户端节省流量。
4+
数据压缩,我们需要用到二个Java类,也就是java.util.zip 中的
5+
类 GZIPOutputStream
6+
此类为使用 GZIP 文件格式写入压缩数据实现流过滤器。
7+
8+
java.io
9+
类 ByteArrayOutputStream
10+
此类实现了一个输出流,其中的数据被写入一个 byte 数组。缓冲区会随着数据的不断写入而自动增长。可使用 toByteArray() 和 toString() 获取数据。
11+
12+
13+
我们利用GZIPOutputStream(OutputStream out) 使用默认缓冲区大小创建新的输出流。
14+
再用write(byte[] b)将 b.length 个字节写入此输出流。
15+
也就是把数据压缩后写入ByteArrayOutputStream。
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Manifest-Version: 1.0
2+
Class-Path:
3+
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

myGzipWeb/WebRoot/WEB-INF/web.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app version="3.0"
3+
xmlns="http://java.sun.com/xml/ns/javaee"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
6+
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
7+
<display-name></display-name>
8+
9+
<filter>
10+
<filter-name>gzip</filter-name>
11+
<filter-class>cn.hncu.filter.GzipFilter</filter-class>
12+
</filter>
13+
<filter-mapping>
14+
<filter-name>gzip</filter-name>
15+
<url-pattern>/*</url-pattern>
16+
</filter-mapping>
17+
18+
<servlet>
19+
<servlet-name>FirstGzipServlet</servlet-name>
20+
<servlet-class>cn.hncu.servlets.FirstGzipServlet</servlet-class>
21+
</servlet>
22+
<servlet>
23+
<servlet-name>SecondServlet</servlet-name>
24+
<servlet-class>cn.hncu.servlets.SecondServlet</servlet-class>
25+
</servlet>
26+
<servlet>
27+
<servlet-name>ThirdServlet</servlet-name>
28+
<servlet-class>cn.hncu.servlets.ThirdServlet</servlet-class>
29+
</servlet>
30+
31+
32+
33+
<servlet-mapping>
34+
<servlet-name>FirstGzipServlet</servlet-name>
35+
<url-pattern>/FirstGzipServlet</url-pattern>
36+
</servlet-mapping>
37+
<servlet-mapping>
38+
<servlet-name>SecondServlet</servlet-name>
39+
<url-pattern>/servlet/SecondServlet</url-pattern>
40+
</servlet-mapping>
41+
<servlet-mapping>
42+
<servlet-name>ThirdServlet</servlet-name>
43+
<url-pattern>/servlet/ThirdServlet</url-pattern>
44+
</servlet-mapping>
45+
<welcome-file-list>
46+
<welcome-file>index.jsp</welcome-file>
47+
</welcome-file-list>
48+
</web-app>

myGzipWeb/WebRoot/index.jsp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
2+
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
3+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4+
<html>
5+
<head>
6+
<title>过滤器应用示例---全站压缩</title>
7+
</head>
8+
9+
<body>
10+
<h2>过滤器应用示例---全站压缩</h2>
11+
<a href="<c:url value='/FirstGzipServlet'/>">请求第一个servlet--网页压缩输出的基本原理</a> <br/><br/>
12+
<a href="<c:url value='/servlet/SecondServlet'/>">请求第二个servlet--用过滤器压缩输出字节流数据</a><br/><br/>
13+
<a href="<c:url value='/servlet/ThirdServlet'/>">请求第三个servlet--用过滤器压缩输出字符流数据</a><br/><br/>
14+
</body>
15+
</html>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package cn.hncu.filter;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.io.OutputStreamWriter;
7+
import java.io.PrintWriter;
8+
import java.util.zip.GZIPOutputStream;
9+
10+
import javax.servlet.Filter;
11+
import javax.servlet.FilterChain;
12+
import javax.servlet.FilterConfig;
13+
import javax.servlet.ServletException;
14+
import javax.servlet.ServletOutputStream;
15+
import javax.servlet.ServletRequest;
16+
import javax.servlet.ServletResponse;
17+
import javax.servlet.http.HttpServletResponse;
18+
import javax.servlet.http.HttpServletResponseWrapper;
19+
20+
public class GzipFilter implements Filter {
21+
22+
@Override
23+
public void init(FilterConfig filterConfig) throws ServletException {
24+
}
25+
26+
@Override
27+
public void doFilter(ServletRequest request, ServletResponse response,
28+
FilterChain chain) throws IOException, ServletException {
29+
30+
MyResponse resp = new MyResponse((HttpServletResponse) response);
31+
// 用增强版的resp放行到servlet中去用(让后台把数据 写到 baout中 )
32+
33+
chain.doFilter(request, resp);// 放行--让后台去写
34+
35+
// 从增强版的resp的baout中(存放的是源字节数据),把数据取出来进行压缩,
36+
// 然后再压缩后的数据用原生的response输出到客户端
37+
ByteArrayOutputStream baout = resp.getBaout();
38+
byte bs[] = baout.toByteArray();// 源字节数据
39+
System.out.println("压缩前大小:" + bs.length);
40+
41+
ByteArrayOutputStream baout2 = new ByteArrayOutputStream();
42+
GZIPOutputStream gout = new GZIPOutputStream(baout2);
43+
gout.write(bs);// 把数据压缩到baout中
44+
gout.close();
45+
46+
bs = baout2.toByteArray();
47+
System.out.println("压缩后大小:" + bs.length);
48+
// 输出之前告诉客户端:我们的数据是gzip格式,然后输出
49+
HttpServletResponse httpResp = (HttpServletResponse) response;
50+
httpResp.setHeader("Content-Encoding", "gzip");
51+
httpResp.setContentLength(bs.length);
52+
OutputStream out = httpResp.getOutputStream();
53+
out.write(bs);
54+
55+
}
56+
57+
@Override
58+
public void destroy() {
59+
}
60+
61+
}
62+
63+
class MyResponse extends HttpServletResponseWrapper {
64+
private ByteArrayOutputStream baout = new ByteArrayOutputStream();
65+
66+
public MyResponse(HttpServletResponse response) {
67+
super(response);
68+
}
69+
70+
@Override
71+
public ServletOutputStream getOutputStream() throws IOException {
72+
return new MyOutputStream(baout);
73+
}
74+
75+
public ByteArrayOutputStream getBaout() {
76+
if(pw!=null){
77+
pw.flush();
78+
//这里很重要,如果不flush或close,不把字符流刷出去,baout中是不会有数据的.
79+
}
80+
return baout;
81+
}
82+
83+
private PrintWriter pw;
84+
@Override
85+
public PrintWriter getWriter() throws IOException {
86+
pw = new PrintWriter(new OutputStreamWriter(baout, "utf-8"),true);
87+
return pw;
88+
}
89+
90+
}
91+
92+
class MyOutputStream extends ServletOutputStream {
93+
private ByteArrayOutputStream baout = null;
94+
95+
public MyOutputStream(ByteArrayOutputStream baout) {
96+
this.baout = baout;
97+
}
98+
99+
@Override
100+
public void write(int b) throws IOException {
101+
baout.write(b);
102+
}
103+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package cn.hncu.servlets;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.IOException;
5+
import java.io.OutputStream;
6+
import java.io.PrintWriter;
7+
import java.util.zip.GZIPOutputStream;
8+
9+
import javax.servlet.ServletException;
10+
import javax.servlet.http.HttpServlet;
11+
import javax.servlet.http.HttpServletRequest;
12+
import javax.servlet.http.HttpServletResponse;
13+
14+
public class FirstGzipServlet extends HttpServlet {
15+
16+
public void doGet(HttpServletRequest request, HttpServletResponse response)
17+
throws ServletException, IOException {
18+
doPost(request, response);
19+
}
20+
21+
public void doPost(HttpServletRequest request, HttpServletResponse response)
22+
throws ServletException, IOException {
23+
24+
response.setContentType("text/html;charset=utf-8");
25+
26+
String str = "网页压缩数据hncuhdncu网页压缩数据hncuh数据hncuhdncu网页压缩数据hncuh数据hncuhdncu网页压缩数据hncuhdncu网页压缩数据hncuhdncu";
27+
28+
System.out.println("原大小:" + str.getBytes("utf-8").length);
29+
30+
// 压缩输出--把源数据压缩输出到baout内存流中
31+
ByteArrayOutputStream baout = new ByteArrayOutputStream();
32+
33+
GZIPOutputStream gout = new GZIPOutputStream(baout);
34+
gout.write(str.getBytes("utf-8"));
35+
gout.close();
36+
37+
// 从baout内存流中把压缩后的数据取出来,输出到客户端
38+
byte dest[] = baout.toByteArray();
39+
System.out.println("压缩后的大小:" + dest.length);
40+
41+
//输出之前告诉客户端:我们的数据是gzip格式,然后输出
42+
response.setHeader("Content-Encoding", "gzip");
43+
response.setContentLength(dest.length);
44+
OutputStream out = response.getOutputStream();
45+
out.write(dest);
46+
out.close();
47+
}
48+
49+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package cn.hncu.servlets;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.io.PrintWriter;
6+
7+
import javax.servlet.ServletException;
8+
import javax.servlet.http.HttpServlet;
9+
import javax.servlet.http.HttpServletRequest;
10+
import javax.servlet.http.HttpServletResponse;
11+
12+
public class SecondServlet extends HttpServlet {
13+
public void doGet(HttpServletRequest request, HttpServletResponse response)
14+
throws ServletException, IOException {
15+
doPost(request, response);
16+
}
17+
18+
public void doPost(HttpServletRequest request, HttpServletResponse response)
19+
throws ServletException, IOException {
20+
21+
response.setContentType("text/html;charset=utf-8");
22+
String str = "网页压缩数据hncuhdncu网页压缩数据hncuh数据hncuhdncu网页压缩数据hncuh数据hncuhdncu网页压缩数据hncuhdncu网页压缩数据hncuhdncu";
23+
System.out.println("原大小:" + str.getBytes("utf-8").length);
24+
25+
OutputStream out = response.getOutputStream();
26+
out.write(str.getBytes("utf-8"));
27+
//注意,虽然MyEclipse环境设置的是utf-8编码,但本句“str.getBytes()”却是以gbk方式编码---应该是Tomcat中的JVM采用的方式
28+
}
29+
}

0 commit comments

Comments
 (0)