码迷,mamicode.com
首页 > 其他好文 > 详细

服务器将数据以压缩的形式,返回给浏览器

时间:2015-08-14 20:53:28      阅读:112      评论:0      收藏:0      [点我收藏+]

标签:

servlet的实现

//压缩输出
public class ServletDemo2 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String data = "aaaaaaaa";
        
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        GZIPOutputStream gout = new GZIPOutputStream(bout);
        gout.write(data.getBytes());
        gout.close();
        
        
        byte gzip[] = bout.toByteArray();
        response.setHeader("content-encoding", "gzip");
        response.setHeader("content-length", gzip.length + "");
        
        response.getOutputStream().write(gzip);
        
        
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

 

服务器将数据以压缩的形式,返回给浏览器

标签:

原文地址:http://www.cnblogs.com/zhangbaowei/p/4730888.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!