2008年1月20日星期日

Java 获取url对象属性

 
import java.net.*
 
public class Test {
 public static void main(String[] args)  {
  /*
  InetAddress hd=null,hd2=null;
  try{
   hd=InetAddress.getLocalHost();
      hd2=InetAddress.getByName("localhost");
  }catch(UnknownHostException e){}
  System.out.println(hd);
  System.out.println(hd2);
  */
  
  URL url2=null;
  try{
   url2=new URL("http://news.163.com/08/0120/03/42KCLAAS0001124J.html");
  } catch(MalformedURLException e){System.out.println("网址可能有错误"+e);} 
  
  System.out.println("网址是");
  System.out.println(url2);
  System.out.println("传输协议是");
  System.out.println(url2.getProtocol());
  System.out.println("通信端口是:");
  System.out.println(url2.getPort());
  System.out.println("资源文件名称是");
  System.out.println(url2.getFile());
  System.out.println("参考点");
  System.out.println(url2.getRef());
  
 }
 
}
 
 
输出为:
 
网址是
http://news.163.com/08/0120/03/42KCLAAS0001124J.html
传输协议是
http
通信端口是:
-1
资源文件名称是
/08/0120/03/42KCLAAS0001124J.html
参考点
null

没有评论: