是否为GZIPInputStream格式(gzip压缩格式)

判断是否为GZIPInputStream格式(gzip压缩格式)

转自 http://wingware.iteye.com/blog/1618561

Java代码   收藏代码
  1.               InputStream ips = null;  
  2. // 取前两个字节  
  3. byte[] header = new byte[2];  
  4. if (isGzip()) {  
  5.     try {  
  6.         BufferedInputStream bis = new BufferedInputStream(connection.getInputStream());  
  7.         bis.mark(2);  
  8.         int result = bis.read(header);  
  9.         // reset输入流到开始位置  
  10.         bis.reset();  
  11.         // 判断是否是GZIP格式  
  12.         int ss = (header[0] & 0xff) | ((header[1] & 0xff) << 8);  
  13.         if(result!=-1 && ss == GZIPInputStream.GZIP_MAGIC) {  
  14.             //System.out.println("为数据压缩格式...");  
  15.             ips= new GZIPInputStream(bis);  
  16.         } else {  
  17.             // 取前两个字节  
  18.             ips= bis;  
  19.         }  
  20.     } catch (java.io.IOException e) {  
  21.         e.printStackTrace();  
  22.         ips = connection.getInputStream();  
  23.     }  
  24. else {  
  25.     ips = connection.getInputStream();  
  26. }  


判断header中是否包含有gzip 
Java代码   收藏代码
  1. public boolean isGzip() {  
  2.         boolean gzip = false;  
  3.         for (String key : this.getHeaders().keySet()) {  
  4.             if (key.equalsIgnoreCase("Accept-Encoding") && this.getHeaders().get(key).contains("gzip")) {  
  5.                 gzip = true;  
  6.                 break;  
  7.             }  
  8.         }  
  9.         return gzip;  
  10.     }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值