java URI

URI是对URL的抽象,不仅包括统一资源定位符,还包括统一资源名,在java中URI用java.net.URI类表示,这个类与java.net.URL类的区别表现在:

  • URI类完全有关于资源的标识和URI的解析,他没有提供方法来获取URI所标识的资源的标识
  • 相比URL类,URI类与相关的规范更一致
  • URI对象可以表示相对URI,URI类在存储URI之前会将其绝对化

简而言之,URL对象时对应网络获取的应用层协议的一个表示,二URI对象纯粹用于解析和处理字符串.

 URI构造函数的用法

import java.awt.image.ImageProducer;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.net.*;

public class Url {

    public static void main(String[] args) throws MalformedURLException, URISyntaxException {
        
        URI u1 = new URI("http://www.baidu.com");
        URI u2 = new URI("http","//www.baidu.com",null);
        URI u3 = new URI("http","//www.baidu.com","today");
        URI u4 = new URI(null,"//www.baidu.com","today");//创建一个相对URI
        URI u5 = new URI("http","//www.baidu.com","<");//片段标识赋符中禁止的字符自动转义
        URI u6 = new URI("http","//www.baidu.com","/javafaq/index.html","referrer=cnet&date=2014-02-23","<");//加入了chax字符部分
        URI u7 = new URI("ftp","anoymous:elharo#ibiblio.org","ftp.oreilly.com",21,"/javafaq/index.html",null,null);
        System.out.println(u1);
        System.out.println(u2);
        System.out.println(u3);
        System.out.println(u4);
        System.out.println(u5);
        System.out.println(u6);
        System.out.println(u7);
        
    }
}/* output:
http://www.baidu.com
http://www.baidu.com
http://www.baidu.com#today
//www.baidu.com#today
http://www.baidu.com#%3C
http://%2F%2Fwww.baidu.com/javafaq/index.html?referrer=cnet&date=2014-02-23#%3C
ftp://anoymous:elharo%23ibiblio.org@ftp.oreilly.com:21/javafaq/index.html

*///~

 

posted @ 2019-03-11 23:34  江期玉  阅读(2109)  评论(1编辑  收藏  举报