Gerry的专栏

一切从现在开始

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

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

 
阅读更多
个人分类: Java技术
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭