现在的位置: 首页 > 综合 > 正文

一个简单的获取URL 数据方法

2013年08月14日 ⁄ 综合 ⁄ 共 1161字 ⁄ 字号 评论关闭

package com.具体的包明;

import java.net.URLConnection;
import java.net.URL;
import java.io.InputStream;
import java.io.ByteArrayOutputStream;

/**
 * Created by IntelliJ IDEA.
 * User: jiniwang
 * Date: 2005-11-8
 * Time: 19:52:14
 * To change this template use File | Settings | File Templates.
 */
public class SnatchData {
     URLConnection conn = null;
     String all_content;

    public String getData(String str_url) throws Exception{
 try {
            all_content =new  String();
            URL url = new URL(str_url);
            conn = url.openConnection();

            if (conn == null)
                return null;

            InputStream ins = conn.getInputStream();

             ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
            byte[] str_b = new byte[1024];
            try {
                int i = -1;
                while ((i=ins.read(str_b)) > 0) {
                 outputstream.write(str_b,0,i);
                }
                all_content = outputstream.toString();
            } catch (Exception ex) {
                ex.printStackTrace();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
       // return new String(all_content.getBytes("ISO8859-1"));
        return all_content;
    }
}

调用getData(string url) 返回一个string

 

抱歉!评论已关闭.