Java实现GZIP压缩

Java实现GZIP压缩

前段时间需要使用到天气预报数据,从网站下载下来之后,发现全部是乱码,一直不明白怎么回事,后来偶然的机会,在查看响应信息时,发现网站在传输响应数据时,使用了gzip压缩,也就是我一直获取到的是压缩后的数据,未经过解压缩就直接使用,所以一直乱码了。下面简单整理一下:

GZIP最早由Jean-loup Gailly和Mark Adler创建,用于UNⅨ系统的文件压缩。我们在Linux中经常会用到后缀为.gz的文件,它们就是GZIP格式的。现今已经成为Internet上使用非常普遍的一种数据压缩格式,或者说一种文件格式。

HTTP协议上的GZIP编码是一种用来改进WEB应用程序性能的技术。大流量的WEB站点常常使用GZIP压缩技术来让用户感受更快的速度。这一般是指WWW服务器中安装的一个功能,当有人来访问这个服务器中的网站时,服务器中的这个功能就将网页内容压缩后传输到来访的电脑浏览器中显示出来。一般对纯文本内容可压缩到原大小的40%,这样传输就快了,效果就是你点击网址后会很快的显示出来,当然这也会增加服务器的负载,一般服务器中都安装有这个功能模块的。

下面来写一个简单的示例,看看Java中如何实现GZIP压缩与解压缩。

编写一个服务器端的Servlet,用于向客户端响应传输一个文件,使用GZIP压缩传输的数据内容:

package com.gzip.demo;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPOutputStream;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * GZIP
 * 
 * @author 小明
 *
 */
@WebServlet("/download")
public class DownloadServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 从响应中获取输入流对象,客户端从输入流中读取数据,另存到文件中
        InputStream in = new FileInputStream(request.getServletContext().getRealPath("/") + "Kameo-Hero's Theme.wma");
        GZIPOutputStream gzipOut = new GZIPOutputStream(response.getOutputStream());
        byte[] b = new byte[1024];
        int len;
        int total = 0; // 文件总大小
        while ((len = in.read(b)) != -1) {
            gzipOut.write(b, 0, len);
            total += len;
        }

        gzipOut.flush();
        gzipOut.close();
        in.close();
        System.out.println("文件大小:" + total);
    }
}

客户端访问Servlet资源,将获取到的响应数据保存到文件中:

package com.gzip.demo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 下载测试:未使用gzip解压缩
 * 
 * @author 小明
 *
 */
public class Download {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://localhost:8080/GzipDemo/download");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setConnectTimeout(1000);
        httpURLConnection.setDoInput(true);
        httpURLConnection.connect();
        if (httpURLConnection.getResponseCode() == 200){
            InputStream in = httpURLConnection.getInputStream();
            OutputStream out = new FileOutputStream("test.wma");
            byte[] b = new byte[1024];
            int len;
            int total = 0; // 接收到的文件大小
            while((len = in.read(b)) != -1){
                out.write(b, 0, len);
                total += len;
            }
            out.flush();
            out.close();
            in.close();
            System.out.println("响应获取到数据大小:" + total);
            System.out.println("下载完成");
        }
    }
}

对获取到的响应数据未解压缩,查看文件大小:

服务器端:

文件大小:2277129

客户端:

响应获取到数据大小:2231918
下载完成

客户端获取的是压缩后的大小。

使用GZIP解压缩:

package com.gzip.demo;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

/**
 * 下载测试:未使用gzip解压缩
 * 
 * @author 小明
 *
 */
public class Download_GZIP {
    public static void main(String[] args) throws IOException {
        URL url = new URL("http://localhost:8080/GzipDemo/download");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        httpURLConnection.setRequestMethod("GET");
        httpURLConnection.setConnectTimeout(1000);
        httpURLConnection.setDoInput(true);
        httpURLConnection.connect();
        if (httpURLConnection.getResponseCode() == 200){
            InputStream in = new GZIPInputStream(httpURLConnection.getInputStream());
            OutputStream out = new FileOutputStream("test2.wma");
            byte[] b = new byte[1024];
            int len;
            int total = 0; // 总大小
            while((len = in.read(b)) != -1){
                out.write(b, 0, len);
                total += len;
            }
            out.flush();
            out.close();
            in.close();
            System.out.println("接收到响应数据大小:" + total);
            System.out.println("下载成功");
        }
    }
}

客户端打印结果:

接收到响应数据大小:2277129
下载成功

示例是压缩的wma文件,如果压缩的是文本文件,则压缩率会更高。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值