package com.jay.http.test;

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 ServletTwo extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String data = "Fresh air and sunshine can have an amazing effect on our feelings. "
				+ "Sometimes when we are feeling down, all that we need to do is simply to go "
				+ "outside and breathe. Movement and exercise is also a fantastic way to feel better. "
				+ "Positive emotions can be generated by motion. So if we start to feel down,"
				+ " take some deep breathes, go outside, feel the fresh air, "
				+ "let the sun hit our face, go for a hike, a walk, a bike ride, "
				+ "a swim, a run, whatever. We will feel better if we do this.";
		System.out.println("原始数据长度:" + data.getBytes().length);
		// 对数据进行压缩:
		ByteArrayOutputStream bout = new ByteArrayOutputStream();
		GZIPOutputStream gout = new GZIPOutputStream(bout);
		gout.write(data.getBytes());
		gout.close();
		// 得到压缩后的数据
		byte gdata[] = bout.toByteArray();
		resp.setHeader("Content-Encoding", "gzip");
		resp.setHeader("Content-Length", gdata.length + "");
		resp.getOutputStream().write(gdata);

	}

	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	};
}
