【问题标题】:Difference between opaque and hierarchical URI?不透明和分层 URI 之间的区别?
【发布时间】:2015-10-16 15:06:02
【问题描述】:

java networking 的上下文中opaquehierarchical URI 有什么区别?

【问题讨论】:

    标签: java uri


    【解决方案1】:

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

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

    一些例子:

    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]
    

    【讨论】:

      【解决方案2】:

      这由 URI 类的 javadoc 解释:

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

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

      除此之外,根据相关规范,“差异”还包括不透明 URI 和分层 URI 之间的固有差异;例如

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

      【讨论】:

        猜你喜欢
        • 2015-05-10
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        • 2023-03-08
        • 1970-01-01
        • 1970-01-01
        • 2019-11-10
        相关资源
        最近更新 更多