通过网络获取页面返回的数据流

从网络中获取网页数据时,网页有可能使用GZIP压缩技术对页面进行压缩,这样就会减小通过网络传输的数据量,提高浏览的速度。因此在获取网络数据时要对其进行判断,对GZIP格式的数据使用GZIPInputStream对其特殊处理,否则在获取数据可能出现乱码.

import java.io.ByteArrayOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.zip.GZIPInputStream;

import com.lowagie.text.pdf.codec.Base64.InputStream;

/**
 * 从网络中获取网页数据
 * @author 杜文俊
 */

public class stretest {
	@SuppressWarnings("static-access")
    public static void main(String[] args) throws Exception {
        String result = "";
        URL url = new URL("http://www.yancao18.com/server/getnode.php?user=test&pwd=test");
        //URL url = new URL("http://www.ku6.com/");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(6* 1000);//设置连接超时xml 实体对象
        if (conn.getResponseCode() != 200) throw new RuntimeException("请求url失败");
        InputStream inputStream = (InputStream) conn.getInputStream();//得到网络返回的输入流
        if("gzip".equals(conn.getContentEncoding())){
            result = readDataForZgip(inputStream, "GBK");
        }else {
            result = readData(inputStream, "GBK");
        }
        conn.disconnect();
        System.out.println(result);
        System.err.println("ContentEncoding: " + conn.getContentEncoding());
    }
    
    //第一个参数为输入流,第二个参数为字符集编码
    public static String readData(InputStream inSream, String charsetName) throws Exception{
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;
        while( (len = inSream.read(buffer)) != -1 ){
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        inSream.close();
        return new String(data, charsetName);
    }
    
    //第一个参数为输入流,第二个参数为字符集编码
    public static String readDataForZgip(InputStream inStream, String charsetName) throws Exception{
        GZIPInputStream gzipStream = new GZIPInputStream(inStream);
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        byte[] buffer =new  byte[1024];
        int len = -1;
        while ((len = gzipStream.read(buffer))!=-1) {
            outStream.write(buffer, 0, len);
        }
        byte[] data = outStream.toByteArray();
        outStream.close();
        gzipStream.close();
        inStream.close();
        return new String(data, charsetName);
    }

}


 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值