import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServletDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte b[] = data.getBytes();
System.out.println("原始数据的大小:" + b.length);
// GZIP压缩
// 创建一个带有缓存的内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(out);
gout.write(b);
gout.close();
// 输出压缩到缓冲流中的数据(响应正文)
b = out.toByteArray();
System.out.println("压缩后的数据大小:" + b.length);
// 通知客户端数据的压缩编码
response.setHeader("Content-Encoding", "gzip");
// 通知客户端响应正文的数据大小,静态页面响应速度快
response.setHeader("Content-Length", b.length+"");
response.getOutputStream().write(b);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
import java.io.IOException;
import java.util.zip.GZIPOutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MyServletDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
byte b[] = data.getBytes();
System.out.println("原始数据的大小:" + b.length);
// GZIP压缩
// 创建一个带有缓存的内存输出流
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(out);
gout.write(b);
gout.close();
// 输出压缩到缓冲流中的数据(响应正文)
b = out.toByteArray();
System.out.println("压缩后的数据大小:" + b.length);
// 通知客户端数据的压缩编码
response.setHeader("Content-Encoding", "gzip");
// 通知客户端响应正文的数据大小,静态页面响应速度快
response.setHeader("Content-Length", b.length+"");
response.getOutputStream().write(b);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}