notruiyi的专栏

物价猛涨,液化气超过100了

原创 获取远程文件1-使用URLConnection收藏

URLConnection用来获取远程文件一般是服务器开放指名服务的情况下,比如http,ftp等

由于本代码比较简单所以省略uml图

-------------------------------------------------------------------------------------------------------------

import java.io.*;

import java.net.*;

public class URLClient{

    public String getDocumentAt(String urlString){

        StringBuffer content = new StringBuffer();

        try{

            URL url = new URL(urlString);

            URLConnection connection = url.getConnection();

            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));

           String line = null;

          while((line = reader.readLine())!=null)

                content.append(line+"\n");

            reader.close();    

        }catch(MalformedURLException e){

              System.out.println("URL Error.");

        }catch(IOException e){

             System.out.println("IOException occured while connecting .");

        }

        return content.toString();

    }

    public static void main(String[] args){

        URLClient client = new URLClient();

        String yahoo = client.getDocumentAt("http://cn.yahoo.com");

        System.out.print(yahoo);

    }

}

发表于 @ 2005年10月22日 13:47:00|评论(loading...)

新一篇: 获取远程文件2-socket循环式服务器 | 

Csdn Blog version 3.1a
Copyright © notruiyi