一个Demo看URL和URI的区别

DEMO来自:http://blog.sina.com.cn/s/blog_621c16b101012itu.html

package com.practise.test;

import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Created by mashao on 15/11/27.
 */
public class Main {

    public static void main(String[] args) {
        try {
            URL url=new URL("http://developer.android.com/referencejava/net/URL.html?s=a#getRef()");
            System.out.println("Authority是:"+url.getAuthority());
            System.out.println("Host是:"+url.getHost());
            System.out.println("Port是:"+url.getPort());
            System.out.println("File是"+url.getFile());
            System.out.println("Path是"+url.getPath());
            System.out.println("Query是:"+url.getQuery());
            System.out.println("Ref是:"+url.getRef());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        try {
            URI absolute=new URI("http://www.baidu.com");
            URI relative=new URI("robot.txt");
            URI resolved=new URI("http://www.baidu.com/robot.txt");

            System.out.println("-----------------------解析前-------------------------");
            System.out.println("Authority是:"+relative.getAuthority());
            System.out.println("Host是:"+relative.getHost());
            System.out.println("Port是:"+relative.getPort());
            System.out.println("Path是"+relative.getPath());
            System.out.println("Query是:"+relative.getQuery());
            System.out.println("Ref是:"+relative.getFragment());
            System.out.println("Scheme是:"+relative.getScheme());

            System.out.println("-----------------------解析后-------------------------");
            relative=absolute.resolve(relative);
            System.out.println("Authority是:"+relative.getAuthority());
            System.out.println("Host是:"+relative.getHost());
            System.out.println("Port是:"+relative.getPort());
            System.out.println("Path是"+relative.getPath());
            System.out.println("Query是:"+relative.getQuery());
            System.out.println("Ref是:"+relative.getFragment());
            System.out.println("Scheme是:"+relative.getScheme());

            System.out.println("-----------------------相对化-------------------------");
            relative=absolute.relativize(resolved);
            System.out.println("Authority是:"+relative.getAuthority());
            System.out.println("Host是:"+relative.getHost());
            System.out.println("Port是:"+relative.getPort());
            System.out.println("Path是"+relative.getPath());
            System.out.println("Query是:"+relative.getQuery());
            System.out.println("Ref是:"+relative.getFragment());
            System.out.println("Scheme是:"+relative.getScheme());

            System.out.println("-----------------------不透明-------------------------");
            URI uri=new URI("mailto:warsh@msncn");
            System.out.println("Authority是:"+uri.getAuthority());
            System.out.println("Host是:"+uri.getHost());
            System.out.println("Port是:"+uri.getPort());
            System.out.println("Path是"+uri.getPath());
            System.out.println("Query是:"+uri.getQuery());
            System.out.println("Ref是:"+uri.getFragment());
            System.out.println("Scheme是:"+uri.getScheme());
            System.out.println("Scheme具体部分:"+uri.getSchemeSpecificPart());

        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

总结:URI是你给一个东西的标识的名字,而URL是你给这个东西的地址用来找到这样东西。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值