不透明和分层URI之间的区别?

时间:2015-07-26 10:37:53

标签: java uri

java networking上下文中不透明分层URI 的区别是什么?

2 个答案:

答案 0 :(得分:15)

不透明uri的典型示例是邮件到网址mailto:a@b.com。它们与分层uri的不同之处在于它们不描述资源的路径。

因此,不透明的Uri会为null返回getPath

一些例子:

public static void main(String[] args) {
    printUriInfo(URI.create("mailto:a@b.com"));
    printUriInfo(URI.create("http://example.com"));
    printUriInfo(URI.create("http://example.com/path"));
    printUriInfo(URI.create("scheme://example.com"));
    printUriInfo(URI.create("scheme:example.com"));
    printUriInfo(URI.create("scheme:example.com/path"));
    printUriInfo(URI.create("path"));
    printUriInfo(URI.create("/path"));
}

private static void printUriInfo(URI uri) {
    System.out.println(String.format("Uri [%s]", uri));
    System.out.println(String.format(" is %sopaque", uri.isOpaque() ? "" : "not "));
    System.out.println(String.format(" is %sabsolute", uri.isAbsolute() ? "" : "not "));
    System.out.println(String.format(" Path [%s]", uri.getPath()));
}

打印:

Uri [mailto:a@b.com]
 is opaque
 is absolute
 Path [null]
Uri [http://example.com]
 is not opaque
 is absolute
 Path []
Uri [http://example.com/path]
 is not opaque
 is absolute
 Path [/path]
Uri [scheme://example.com]
 is not opaque
 is absolute
 Path []
Uri [scheme:example.com]
 is opaque
 is absolute
 Path [null]
Uri [scheme:example.com/path]
 is opaque
 is absolute
 Path [null]
Uri [path]
 is not opaque
 is not absolute
 Path [path]
Uri [/path]
 is not opaque
 is not absolute
 Path [/path]

答案 1 :(得分:3)

这可以通过javadoc解释URI类:

  

“URI是不透明的,当且仅当它是绝对的且其特定于方案的部分不以斜杠字符('/')开头。不透明的URI有一个方案,a特定于方案的部分,可能还有一个片段;所有其他组件都未定义。

它所指的“组件”是各种URI getter返回的值。

除此之外,“差异”包括根据相关规范在不透明和分层URI之间的固有差异; e.g。

这些差异绝不是Java特有的。

相关问题