码迷,mamicode.com
首页 > Web开发 > 详细

URL

时间:2017-06-23 23:53:30      阅读:421      评论:0      收藏:0      [点我收藏+]

标签:spec   路径   system   reading   获取   java   res   handler   lin   

  url,uniform resource locator,统一资源定位符,指示互联网上的资源,通常我们称之为网页地址。url可以分为协议、主机、端口号(http默认为80)、文件路径、请求参数、定位位置。

  java语言中可以使用URL类在internet上获取网络资源。它有如下6个构造方法:

1.URL(String spec)
2.URL(String protocol, String host, int port, String file)
3.URL(String protocol, String host, int port, String file, URLStreamHandler handler)
4.URL(String protocol, String host, string file)
5.URL(URL context, String spec)
6.URL(URL context, String spec, URLStreamHandler handler)

  从这些构造函数可以看出,可以使用一个绝对的url构成的字符串来构造URL,也可以使用url的各个部分构造,或者使用相对的url构造。如下是一个简单的示例程序:

class operateURL
{
         static protected void getURL(String u)
         {
                   try{
                           System.out.println("Reading URL: "+u);
                           URL url = new URL(u);
                           InputStream is = url.openStream();
                           InputStreamReader isr = new InputStreamReader(is);
                           BufferedReader r = new BufferedReader(isr);
                           String s;
                           do{
                                  s = r.readLine();
                                  if(s!=null)
                                            System.out.println(s);
                           }while(s!=null);
                    }catch(MalformedURLException e)
                    {
                           System.out.println("Must be a valid url");
                    }
                    catch(IOException e)
                    {
                            System.out.println("Can not connect");
                    }
         }    
}        

   

  

URL

标签:spec   路径   system   reading   获取   java   res   handler   lin   

原文地址:http://www.cnblogs.com/canyudeguang/p/7071973.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!