随笔-295  评论-26  文章-1  trackbacks-0

import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
/*
 * hello.java
 *
 * Created on 2007年8月3日, 上午11:15
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

/**
 *
 * @author ljl
 */
public class hello {
   
    /** Creates a new instance of hello */
    public hello() {
    }
    public static void main(String s[]) throws Exception{
        String sss="Sogood";
       // System.out.print(sss.substring(0,4));
        ReadURL("http://java.ok6.org/logo.jpg");
    }
    public static void ReadURL(String URLName) throws Exception // 如果发生异常则向上抛出
    {
        int HttpResult; // 服务器返回的状态
        URL url =new URL(URLName); // 创建URL
       
        URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
        urlconn.connect();
        HttpURLConnection httpconn =(HttpURLConnection)urlconn;
        HttpResult = httpconn.getResponseCode();
        System.out.println(HttpResult);
        if(HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK说明连接不成功
           System.out.print("fail");
        else {
            int filesize = urlconn.getContentLength(); // 取数据长度
            System.out.println(filesize);
            InputStreamReader isReader = new InputStreamReader(urlconn.getInputStream());
            char[] buffer = new char[2048]; // 创建存放输入流的缓冲
            int num = 0; // 读入的字节数
            while(num>-1) {
                num = isReader.read(buffer); // 读入到缓冲区
                if(num < 0) break; // 已经读完
                System.out.print("suc");
              System.out.println(new String(buffer,0,num)); // 显示出来
            }
            isReader.close();//关闭输入流
        }
    }
   
   
}



大盘预测 国富论
posted on 2007-08-21 10:38 华梦行 阅读(978) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: