Most visited

Recently visited

Added in API level 1

URI

public final class URI
extends Object implements Comparable<URI>, Serializable

java.lang.Object
   ↳ java.net.URI


表示统一资源标识符(URI)参考。

除了下面指出的一些微小偏差,这个类的一个实例表示由所定义的URI引用RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax ,通过修正RFC 2732: Format for Literal IPv6 Addresses in URLs Literal IPv6地址格式还支持scope_ids。 scope_ids的语法和用法描述为here 该类提供了用于从其组件创建URI实例的构造函数,或者通过解析它们的字符串形式,访问实例的各种组件的方法以及用于规范化,解析和相对化URI实例的方法。 这个类的实例是不可变的。

URI syntax and components

At the highest level a URI reference (hereinafter simply "URI") in string form has the syntax
[ scheme : ] scheme-specific-part[ # fragment]
where square brackets [...] delineate optional components and the characters : and # stand for themselves.

绝对 URI指定一个方案; 一个不是绝对的URI被认为是相对的 URI也根据其是不透明还是分层进行分类

不透明 URI是绝对URI,其特定于方案的部分不以斜杠字符开头( '/' )。 不透明的URI不需要进一步解析。 不透明URI的一些例子是:

mailto:java-net@java.sun.com
news:comp.lang.java
urn:isbn:096139210x

分层 URI既可以是绝对URI,其特定于方案的部分以斜杠字符开始,也可以是相对URI,即不指定方案的URI。 分层URI的一些例子是:

http://java.sun.com/j2se/1.3/
docs/guide/collections/designfaq.html#28
../../../demo/jfc/SwingSet2/src/SwingSet2.java
file:///~/calendar

根据语法,分层URI可以进一步解析

[ scheme :][ // authority][ path][ ? query][ # fragment]
where the characters :, /, ?, and # stand for themselves. The scheme-specific part of a hierarchical URI consists of the characters between the scheme and fragment components.

如果指定了分层URI的权威组件,则可以是基于服务器的 ,也可以是基于 注册表的 基于服务器的权限根据熟悉的语法进行解析

[ user-info @] host[ : port]
where the characters @ and : stand for themselves. Nearly all URI schemes currently in use are server-based. An authority component that does not parse in this way is considered to be registry-based.

如果以斜杠字符开头( '/' ),则分层URI的路径组件本身被认为是绝对路径 ; 否则它是相对的。 绝对或指定权限的层次结构URI的路径始终是绝对路径。

总而言之,URI实例具有以下九个组件:

Component Type
scheme String
scheme-specific-part     String
authority String
user-info String
host String
port int
path String
query String
fragment String
In a given instance any particular component is either undefined or defined with a distinct value. Undefined string components are represented by null, while undefined integer components are represented by -1. A string component may be defined to have the empty string as its value; this is not equivalent to that component being undefined.

某个特定组件是否在实例中定义取决于所代表的URI的类型。 绝对URI有一个scheme组件。 一个不透明的URI有一个方案,一个方案特定的部分,可能还有一个片段,但没有其他组件。 一个分层的URI总是有一个路径(虽然它可能是空的)和一个特定于方案的部分(它至少包含路径),并且可能具有任何其他组件。 如果权威组件存在并且是基于服务器的,那么主机组件将被定义,并且可以定义用户信息和端口组件。

Operations on URI instances

The key operations supported by this class are those of normalization, resolution, and relativization.

规范化是从分层URI的路径组件中删除不必要的"."".."段的过程。 每个"."分段都被简单地删除。 ".."段只有在前面有非".."段时才会被删除。 规范化对不透明的URI没有影响。

解决方法是将一个URI解析为另一个基本 URI的过程。 得到的URI是由RFC 2396指定的方式从这两个URI的组件构造的,从基础URI中取得原始组件中未指定的组件。 对于分层URI,原始路径将根据基本路径进行解析,然后进行归一化。 例如,结果的结果

docs/guide/collections/designfaq.html#28          (1)
against the base URI http://java.sun.com/j2se/1.3/ is the result URI
http://java.sun.com/j2se/1.3/docs/guide/collections/designfaq.html#28
Resolving the relative URI
../../../demo/jfc/SwingSet2/src/SwingSet2.java    (2)
against this result yields, in turn,
http://java.sun.com/j2se/1.3/demo/jfc/SwingSet2/src/SwingSet2.java
Resolution of both absolute and relative URIs, and of both absolute and relative paths in the case of hierarchical URIs, is supported. Resolving the URI file:///~calendar against any other URI simply yields the original URI, since it is absolute. Resolving the relative URI (2) above against the relative base URI (1) yields the normalized, but still relative, URI
demo/jfc/SwingSet2/src/SwingSet2.java

相对化最后是解决方案的逆向:对于任何两个规范化的URI uv

u .relativize( u .resolve( v )).equals( v )  and
u .resolve( u .relativize( v )).equals( v )  .
This operation is often useful when constructing a document containing URIs that must be made relative to the base URI of the document wherever possible. For example, relativizing the URI
http://java.sun.com/j2se/1.3/docs/guide/index.html
against the base URI
http://java.sun.com/j2se/1.3
yields the relative URI docs/guide/index.html.

Character categories

RFC 2396 specifies precisely which characters are permitted in the various components of a URI reference. The following categories, most of which are taken from that specification, are used below to describe these constraints:
alpha The US-ASCII alphabetic characters, 'A' through 'Z' and 'a' through 'z'
digit The US-ASCII decimal digit characters, '0' through '9'
alphanum All alpha and digit characters
unreserved     All alphanum characters together with those in the string "_-!.~'()*"
punct The characters in the string ",;:$&+="
reserved All punct characters together with those in the string "?/[]@"
escaped Escaped octets, that is, triplets consisting of the percent character ('%') followed by two hexadecimal digits ('0'-'9', 'A'-'F', and 'a'-'f')
other The Unicode characters that are not in the US-ASCII character set, are not control characters (according to the Character.isISOControl method), and are not space characters (according to the Character.isSpaceChar method)  (Deviation from RFC 2396, which is limited to US-ASCII)

所有合法的URI字符集都由非 保留保留转义其他字符组成。

Escaped octets, quotation, encoding, and decoding

RFC 2396 allows escaped octets to appear in the user-info, path, query, and fragment components. Escaping serves two purposes in URIs: These purposes are served in this class by three related operations: These operations are exposed in the constructors and methods of this class as follows:

Identities

For any URI u, it is always the case that
new URI( u .toString()).equals( u ) .
For any URI u that does not contain redundant syntax such as two slashes before an empty authority (as in file:///tmp/ ) or a colon following a host name but no port (as in http://java.sun.com: ), and that does not encode characters except those that must be quoted, the following identities also hold:
new URI( u .getScheme(),
        
u .getSchemeSpecificPart(),
        
u .getFragment())
.equals(
u )
in all cases,
new URI( u .getScheme(),
        
u .getUserInfo(),  u .getAuthority(),
        
u .getPath(),  u .getQuery(),
        
u .getFragment())
.equals(
u )
if u is hierarchical, and
new URI( u .getScheme(),
        
u .getUserInfo(),  u .getHost(),  u .getPort(),
        
u .getPath(),  u .getQuery(),
        
u .getFragment())
.equals(
u )
if u is hierarchical and has either no authority or a server-based authority.

URIs, URLs, and URNs

A URI is a uniform resource identifier while a URL is a uniform resource locator. Hence every URL is a URI, abstractly speaking, but not every URI is a URL. This is because there is another subcategory of URIs, uniform resource names (URNs), which name resources but do not specify how to locate them. The mailto, news, and isbn URIs shown above are examples of URNs.

URI和URL之间的概念区别反映在这个类和 URL类之间的差异中。

这个类的一个实例代表RFC 2396定义的语义意义上的一个URI引用。一个URI可以是绝对的也可以是相对的。 根据通用语法解析URI字符串,而不考虑它指定的方案(如果有)。 不执行主机查找(如果有的话),并且不构造依赖于方案的流处理程序。 平等,哈希和比较严格地根据实例的字符内容来定义。 换句话说,一个URI实例只不过是一个结构化的字符串,它支持比较,规范化,解析和相对化的语法,独立于方案的操作。

相反, URL类的一个实例表示URL的语法组件以及访问其描述的资源所需的一些信息。 URL必须是绝对的,也就是说,它必须始终指定一个方案。 URL字符串根据其方案进行解析。 一个流处理程序总是为一个URL建立的,事实上,不可能为一个没有处理程序可用的方案创建一个URL实例。 平等和散列取决于主机的方案和互联网地址(如果有的话); 比较没有定义。 换句话说,URL是一个结构化字符串,它支持解析的语法操作,以及查找主机并打开到指定资源的连接的网络I / O操作。

也可以看看:

Summary

Public constructors

URI(String str)

通过解析给定的字符串构造一个URI。

URI(String scheme, String userInfo, String host, int port, String path, String query, String fragment)

根据给定的组件构造一个分层的URI。

URI(String scheme, String authority, String path, String query, String fragment)

根据给定的组件构造一个分层的URI。

URI(String scheme, String host, String path, String fragment)

根据给定的组件构造一个分层的URI。

URI(String scheme, String ssp, String fragment)

根据给定的组件构造一个URI。

Public methods

int compareTo(URI that)

将此URI与另一个必须是URI的对象进行比较。

static URI create(String str)

通过解析给定的字符串来创建一个URI。

boolean equals(Object ob)

测试此URI是否与另一个对象相等。

String getAuthority()

返回此URI的解码权限组件。

String getFragment()

返回此URI的解码片段组件。

String getHost()

返回此URI的主机组件。

String getPath()

返回此URI的解码路径组件。

int getPort()

返回此URI的端口号。

String getQuery()

返回此URI的解码查询组件。

String getRawAuthority()

返回此URI的原始权限组件。

String getRawFragment()

返回此URI的原始片段组件。

String getRawPath()

返回此URI的原始路径组件。

String getRawQuery()

返回此URI的原始查询组件。

String getRawSchemeSpecificPart()

返回此URI的原始方案特定部分。

String getRawUserInfo()

返回此URI的原始用户信息组件。

String getScheme()

返回此URI的方案组件。

String getSchemeSpecificPart()

返回此URI的解码方案特定部分。

String getUserInfo()

返回此URI的已解码的用户信息组件。

int hashCode()

返回此URI的哈希码值。

boolean isAbsolute()

告诉这个URI是否是绝对的。

boolean isOpaque()

告诉这个URI是不透明的。

URI normalize()

规范化这个URI的路径。

URI parseServerAuthority()

尝试将此URI的权限组件(如果已定义)解析为用户信息,主机和端口组件。

URI relativize(URI uri)

使给定的URI与此URI相对化。

URI resolve(URI uri)

根据此URI解析给定的URI。

URI resolve(String str)

通过解析给定的字符串构造一个新的URI,然后根据这个URI解析它。

String toASCIIString()

以US-ASCII字符串形式返回此URI的内容。

String toString()

以字符串形式返回此URI的内容。

URL toURL()

从这个URI构造一个URL。

Inherited methods

From class java.lang.Object
From interface java.lang.Comparable

Public constructors

URI

Added in API level 1
URI (String str)

通过解析给定的字符串构造一个URI。

这个构造函数完全按照附录A RFC 2396中的语法指定的方式解析给定的字符串, 除了以下偏差之外:

  • 只要后面跟有非空路径,查询组件或片段组件,就可以使用空权限组件。 这允许解析URI,例如"file:///foo/bar" ,这似乎是RFC 2396的意图,尽管语法不允许它。 如果权限组件是空的,那么用户信息,主机和端口组件是未定义的。

  • 空的相对路径是允许的; 这似乎是RFC 2396的意图,尽管语法不允许它。 这种偏差的主要结果是独立片段(例如"#foo")解析为具有空路径和给定片段的相对URI,并且可能对基本URI有用resolved

  • 主机组件中的IPv4地址将严格分析,如RFC 2732所示 :虚线四地址的每个元素必须包含不超过三位十进制数字。 每个元素进一步被限制为不大于255的值。

  • 只包含单个域标签的主机组件中的主机名可以以字母字符开头。 这似乎是RFC 2396第3.2.2节的意图,尽管语法不允许它。 这种偏差的结果是,分层URI(如s://123)的权威组件将作为基于服务器的权限解析。

  • 主机组件允许使用IPv6地址。 IPv6地址必须用RFC 2732指定的方括号( '['']'括起来 IPv6地址本身必须根据RFC 2373解析。 IPv6地址被进一步限制为描述不超过16个字节的地址信息,这是RFC 2373中隐含但在语法中不可表达的约束。

  • 在RFC 2396允许转义字节的地方,即在用户信息,路径,查询和片段组件以及授权组件中,如果授权机构是基于注册表的,则允许其他类别中的字符。 这允许URI包含除US-ASCII字符集以外的Unicode字符。

Parameters
str String: The string to be parsed into a URI
Throws
NullPointerException If str is null
URISyntaxException If the given string violates RFC 2396, as augmented by the above deviations

URI

Added in API level 1
URI (String scheme, 
                String userInfo, 
                String host, 
                int port, 
                String path, 
                String query, 
                String fragment)

根据给定的组件构造一个分层的URI。

如果给出了方案,则路径(如果还给出)必须为空或以斜线字符( '/'开头 否则,通过传递null给相应的参数,或者在port参数的情况下,通过传递-1 ,可以使新URI的组件保持未定义状态

此构造函数首先根据 RFC 2396 ,第5.2节第7步中指定的规则从给定组件构建一个URI字符串:

  1. 最初,结果字符串是空的。

  2. 如果给出方案,则将其附加到结果上,后跟冒号字符( ':' )。

  3. 如果给出用户信息,主机或端口,则附加字符串 "//"

  4. 如果给出了用户信息,那么它将被追加,然后是商业字符( '@' )。 任何不属于无保留标点转义其他类别的字符都是quoted

  5. 如果一个主机被给出,那么它被追加。 如果主机是文字IPv6地址,但没有用方括号括起来( '['']' ),则添加方括号。

  6. 如果给出端口号,则附加冒号字符( ':' ),后跟十进制端口号。

  7. 如果给出了一个路径,那么它被追加。 任何不属于无保留标点符号转义其他类别的字符,并不等于斜线字符( '/' )或商业字符( '@' )。

  8. 如果给出查询,则追加问号字符( '?' ),后跟查询。 引用任何不是legal URI character的字符。

  9. 最后,如果给出一个片段,则会追加一个散列字符( '#' ),后面跟着该片段。 引用任何不是合法URI字符的字符。

然后通过调用URI(String)构造函数然后在结果上调用parseServerAuthority()方法来解析生成的URI字符串; 这可能会导致URISyntaxException被抛出。

Parameters
scheme String: Scheme name
userInfo String: User name and authorization information
host String: Host name
port int: Port number
path String: Path
query String: Query
fragment String: Fragment
Throws
URISyntaxException If both a scheme and a path are given but the path is relative, if the URI string constructed from the given components violates RFC 2396, or if the authority component of the string is present but cannot be parsed as a server-based authority

URI

Added in API level 1
URI (String scheme, 
                String authority, 
                String path, 
                String query, 
                String fragment)

根据给定的组件构造一个分层的URI。

如果给出方案,则路径(如果还给出)必须为空或以斜杠字符( '/'开头 否则,通过为相应的参数传递null ,可以使新URI的组件未定义。

此构造函数首先根据 RFC 2396的第5.2节第7步中指定的规则从给定组件构建一个URI字符串:

  1. 最初,结果字符串是空的。

  2. 如果给出方案,则将其附加到结果上,后跟冒号字符( ':' )。

  3. 如果授权,则附加字符串"//" ,然后是授权。 如果该权威包含文字IPv6地址,则该地址必须用方括号括起来( '['']' )。 任何不属于无保留标点转义其他类别的字符,并且不等于商业字符( '@' ),即quoted

  4. 如果给出了一个路径,那么它被追加。 引用的任何字符都不是毫无保留标点符号转义字符或其他类别,并且不等于斜线字符( '/' )或商业字符( '@' )。

  5. 如果给出查询,则追加问号字符( '?' ),然后是查询。 引用任何不是legal URI character的字符。

  6. 最后,如果给出一个片段,则会附加一个散列字符( '#' ),后跟该片段。 引用任何不是合法URI字符的字符。

然后通过调用URI(String)构造函数然后调用结果parseServerAuthority()方法来解析生成的URI字符串; 这可能会导致URISyntaxException被抛出。

Parameters
scheme String: Scheme name
authority String: Authority
path String: Path
query String: Query
fragment String: Fragment
Throws
URISyntaxException If both a scheme and a path are given but the path is relative, if the URI string constructed from the given components violates RFC 2396, or if the authority component of the string is present but cannot be parsed as a server-based authority

URI

Added in API level 1
URI (String scheme, 
                String host, 
                String path, 
                String fragment)

根据给定的组件构造一个分层的URI。

通过传递 null可能会使组件未定义。

这个便捷构造函数的工作方式如同通过调用七参数构造函数,如下所示:

new URI(scheme, null, host, -1, path, null, fragment);

Parameters
scheme String: Scheme name
host String: Host name
path String: Path
fragment String: Fragment
Throws
URISyntaxException If the URI string constructed from the given components violates RFC 2396

URI

Added in API level 1
URI (String scheme, 
                String ssp, 
                String fragment)

根据给定的组件构造一个URI。

通过传递 null可能会导致组件未定义。

这个构造函数首先使用给定的组件构建一个字符串形式的URI,如下所示:

  1. 最初,结果字符串是空的。

  2. 如果给出方案,则将其附加到结果上,后跟一个冒号字符( ':' )。

  3. 如果给出特定于方案的部分,则将其附加。 任何不是legal URI character的字符都是quoted

  4. 最后,如果给出了一个片段,则将一个散列字符( '#' )附加到字符串后面跟着该片段。 引用任何不是合法URI字符的字符。

然后解析得到的URI字符串,以创建新的URI实例,就好像通过调用URI(String)构造函数一样; 这可能会导致URISyntaxException被抛出。

Parameters
scheme String: Scheme name
ssp String: Scheme-specific part
fragment String: Fragment
Throws
URISyntaxException If the URI string constructed from the given components violates RFC 2396

Public methods

compareTo

Added in API level 1
int compareTo (URI that)

将此URI与另一个必须是URI的对象进行比较。

当比较两个URI的相应组件时,如果一个组件未定义,但另一个组件已定义,则第一个被认为小于第二个。 除非另有说明,否则字符串组件按照String.compareTo方法定义的自然,区分大小写的顺序进行排序。 通过比较它们的原始表单而不是它们的编码形式来比较受编码的字符串组件。

URI的排序定义如下:

  • 两个具有不同方案的URI根据其方案的顺序排序,而不考虑大小写。

  • 层次结构URI被认为小于具有相同方案的不透明URI。

  • 两个具有相同方案的不透明URI根据其方案特定部分的顺序排序。

  • 具有相同方案和方案特定部分的两个不透明URI根据其片段的排序进行排序。

  • 两个具有相同方案的分层URI根据其权限组件的顺序进行排序:

    • 如果两个权限组件都是基于服务器的,那么URI将根据其用户信息组件进行排序; 如果这些组件是相同的,那么根据它们的主机的顺序排列URI,而不考虑大小写; 如果主机是相同的,那么这些URI根据其端口的顺序排序。

    • 如果一个或两个权限组件都是基于注册表的,那么这些URI将根据其权限组件的顺序进行排序。

  • 最后,具有相同方案和权限组件的两个分层URI根据其路径的顺序排序; 如果他们的路径是相同的,那么他们根据他们的查询顺序排序; 如果查询是相同的,那么它们根据其片段的顺序排序。

该方法符合 Comparable.compareTo方法的一般合同。

Parameters
that URI: The object to which this URI is to be compared
Returns
int A negative integer, zero, or a positive integer as this URI is less than, equal to, or greater than the given URI
Throws
ClassCastException If the given object is not a URI

create

Added in API level 1
URI create (String str)

通过解析给定的字符串来创建一个URI。

这个便捷工厂方法就像调用URI(String)构造函数一样工作; 任何由构造函数抛出的URISyntaxException被捕获并包裹在一个新的IllegalArgumentException对象中,然后抛出该对象。

此方法用于已知给定字符串是合法URI的情况,例如,用于在程序中声明的URI常量,因此将被视为不解析字符串的编程错误。 应该使用直接抛出URISyntaxException的构造函数,在这种情况下,用户输入或可能容易出错的其他来源构造URI。

Parameters
str String: The string to be parsed into a URI
Returns
URI The new URI
Throws
NullPointerException If str is null
IllegalArgumentException If the given string violates RFC 2396

equals

Added in API level 1
boolean equals (Object ob)

测试此URI是否与另一个对象相等。

如果给定的对象不是URI,则此方法立即返回 false

对于两个被认为相同的URI要求两者都是不透明的或两者都是分层的。 他们的计划要么不明确,要么不分大小写。 他们的片段必须要么是未定义的,要么是相等的。

对于两个不透明的URI被认为是相等的,他们的方案特定部分必须相等。

对于两个分层的URI被认为是相等的,它们的路径必须相同,并且它们的查询必须是未定义的或者是相等的。 他们的当局必须是未定义的,或者都是基于注册表的,或者都是基于服务器的。 如果他们的权限被定义并且是基于注册表的,那么他们必须是平等的。 如果他们的权限被定义并且是基于服务器的,那么他们的主机必须是平等的,而不考虑大小写,它们的端口号必须相等,并且它们的用户信息组分必须相等。

当测试两个URI的用户信息,路径,查询,片段,权限或特定于计划的部分的相等性时,比较这些组件的原始表单而不是编码形式,并比较转义字节的十六进制数字而不考虑以案件。

该方法符合 Object.equals方法的一般合同。

Parameters
ob Object: The object to which this object is to be compared
Returns
boolean true if, and only if, the given object is a URI that is identical to this URI

getAuthority

Added in API level 1
String getAuthority ()

返回此URI的解码权限组件。

该方法返回的字符串与 getRawAuthority方法返回的字符串相同,只是所有转义八位字节的序列都是 decoded

Returns
String The decoded authority component of this URI, or null if the authority is undefined

getFragment

Added in API level 1
String getFragment ()

返回此URI的解码片段组件。

该方法返回的字符串与 getRawFragment方法返回的字符串相同,只是所有转义八位字节的序列都是 decoded

Returns
String The decoded fragment component of this URI, or null if the fragment is undefined

getHost

Added in API level 1
String getHost ()

返回此URI的主机组件。

URI的主机组件(如果已定义)将具有以下形式之一:

  • 一个域名,由一个或多个由句点字符( '.' )分隔的标签组成,可选地后跟一个句点字符。 每个标签由字母字符和连字符组成( '-' ),尽管连字符不会作为标签中的第一个或最后一个字符出现。 由两个或多个标签组成的域名的最右侧标签以字母开头。

  • 形式 数字 +. +. 数字 +. + ,其中没有 数字序列长于三个字符并且没有序列的点分IPv4地址具有值大于255。

  • 用方括号括起来的IPv6地址( '['']' ),由十六进制数字,冒号( ':' )和可能的嵌入式IPv4地址组成。 IPv6地址的完整语法在RFC 2373: IPv6 Addressing Architecture指定。

The host component of a URI cannot contain escaped octets, hence this method does not perform any decoding.

Returns
String The host component of this URI, or null if the host is undefined

getPath

Added in API level 1
String getPath ()

返回此URI的解码路径组件。

该方法返回的字符串与 getRawPath方法返回的字符串相同,只是所有转义八位字节的序列都是 decoded

Returns
String The decoded path component of this URI, or null if the path is undefined

getPort

Added in API level 1
int getPort ()

返回此URI的端口号。

URI的端口组件(如果已定义)是一个非负整数。

Returns
int The port component of this URI, or -1 if the port is undefined

getQuery

Added in API level 1
String getQuery ()

返回此URI的解码查询组件。

该方法返回的字符串与 getRawQuery方法返回的字符串相同,只是所有转义八位字节的序列都是 decoded

Returns
String The decoded query component of this URI, or null if the query is undefined

getRawAuthority

Added in API level 1
String getRawAuthority ()

返回此URI的原始权限组件。

URI的授权组成,如果定义了仅包含商用在字符('@')和字符在未保留 ,PUNCT, 躲过 ,和其他类别。 如果授权是基于服务器的,那么它将进一步限制拥有有效的用户信息,主机和端口组件。

Returns
String The raw authority component of this URI, or null if the authority is undefined

getRawFragment

Added in API level 1
String getRawFragment ()

返回此URI的原始片段组件。

URI的片段组件(如果已定义)只包含合法的URI字符。

Returns
String The raw fragment component of this URI, or null if the fragment is undefined

getRawPath

Added in API level 1
String getRawPath ()

返回此URI的原始路径组件。

一个URI的路径成分,如果定义了仅包含斜杠字符 ('/'),未保留 ,PUNCT商业-在字符 ('@'),和字符, 逃脱 ,和 其他类别。

Returns
String The path component of this URI, or null if the path is undefined

getRawQuery

Added in API level 1
String getRawQuery ()

返回此URI的原始查询组件。

URI的查询组件(如果已定义)只包含合法的URI字符。

Returns
String The raw query component of this URI, or null if the query is undefined

getRawSchemeSpecificPart

Added in API level 1
String getRawSchemeSpecificPart ()

返回此URI的原始方案特定部分。 特定于方案的部分永远不会被定义,尽管它可能是空的。

URI的方案特定部分只包含合法的URI字符。

Returns
String The raw scheme-specific part of this URI (never null)

getRawUserInfo

Added in API level 1
String getRawUserInfo ()

返回此URI的原始用户信息组件。

URI的用户信息组件(如果已定义)仅包含 未保留标点转义其他类别中的字符。

Returns
String The raw user-information component of this URI, or null if the user information is undefined

getScheme

Added in API level 1
String getScheme ()

返回此URI的方案组件。

URI的scheme组件(如果已定义)只包含alphanum类别和字符串"-.+"中的字符。 一个计划总是以字母开头。

URI的scheme组件不能包含转义字节,因此该方法不执行任何解码。

Returns
String The scheme component of this URI, or null if the scheme is undefined

getSchemeSpecificPart

Added in API level 1
String getSchemeSpecificPart ()

返回此URI的解码方案特定部分。

该方法返回的字符串与 getRawSchemeSpecificPart方法返回的字符串相同,只是所有转义八位字节的序列都是 decoded

Returns
String The decoded scheme-specific part of this URI (never null)

getUserInfo

Added in API level 1
String getUserInfo ()

返回此URI的已解码的用户信息组件。

该方法返回的字符串与 getRawUserInfo方法返回的字符串相同,只是所有转义八位字节的序列都是 decoded

Returns
String The decoded user-information component of this URI, or null if the user information is undefined

hashCode

Added in API level 1
int hashCode ()

返回此URI的哈希码值。 哈希码基于所有URI的组件,并且满足Object.hashCode方法的一般合约。

Returns
int A hash-code value for this URI

isAbsolute

Added in API level 1
boolean isAbsolute ()

告诉这个URI是否是绝对的。

当且仅当具有方案组件时,URI才是绝对的。

Returns
boolean true if, and only if, this URI is absolute

isOpaque

Added in API level 1
boolean isOpaque ()

告诉这个URI是不透明的。

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

Returns
boolean true if, and only if, this URI is opaque

normalize

Added in API level 1
URI normalize ()

规范化这个URI的路径。

如果这个URI是不透明的,或者它的路径已经是普通形式,那么返回这个URI。 否则,构造一个与此URI相同的新URI,不同之处在于它的路径是通过以与RFC 2396 ,第5.2节,第6步,子步骤c至f一致的方式规范化该URI的路径来计算的; 那是:

  1. 所有 "."段都被删除。

  2. 如果".."段的前面是非".."段,则这两个段都将被删除。 重复此步骤直至不再适用。

  3. 如果路径是相对的,并且其第一个段包含冒号字符( ':' ),则会预先创建"."段。 这可以防止具有诸如"a:b/c/d"之类的路径的相对URI从稍后被重新解析为具有方案"a"和方案特定部分"b/c/d"的不透明URI。 (偏离RFC 2396)

如果前面没有足够的非".."段以允许删除,则标准化路径将以一个或多个".."段开始。 如果在上面的步骤3中插入了一条标准化路径,则标准化路径将以"."段开始。 否则,规范化路径将不包含任何"."".."分段。

Returns
URI A URI equivalent to this URI, but whose path is in normal form

parseServerAuthority

Added in API level 1
URI parseServerAuthority ()

尝试将此URI的权限组件(如果已定义)解析为用户信息,主机和端口组件。

如果此URI的权限组件已被识别为基于服务器,那么它已经被解析为用户信息,主机和端口组件。 在这种情况下,或者如果这个URI没有权限组件,这个方法只是返回这个URI。

否则,此方法将再次尝试将权限组件解析为用户信息,主机和端口组件,并抛出一个异常,说明为什么无法以此方式解析权限组件。

提供此方法是因为RFC 2396中指定的通用URI语法不能总是将格式错误的基于服务器的权威与合法的基于注册表的权威区分开来。 因此它必须将前者的一些事例视为后者的实例。 例如,URI字符串"//foo:bar"中的权限组件不是合法的基于服务器的权限,但它作为基于注册表的权限是合法的。

在许多常见情况下,例如,在使用已知为URN或URL的URI时,所使用的分层URI将始终基于服务器。 因此,它们必须被解析或者被视为错误。 在这些情况下,诸如

URI u = new URI(str).parseServerAuthority();

可以用来确保u始终引用一个URI,如果它具有权限组件,则它具有一个具有适当用户信息,主机和端口组件的基于服务器的权限。 调用此方法还可以确保,如果不能以此方式分析权威,那么可以基于抛出的异常发布适当的诊断消息。

Returns
URI A URI whose authority field has been parsed as a server-based authority
Throws
URISyntaxException If the authority component of this URI is defined but cannot be parsed as a server-based authority according to RFC 2396

relativize

Added in API level 1
URI relativize (URI uri)

使给定的URI与此URI相对化。

给定URI与此URI的相对化计算如下:

  1. 如果这个URI或给定的URI是不透明的,或者这两个URI的scheme和authority组件不相同,或者这个URI的路径不是给定URI的路径的前缀,那么给定的URI是回。

  2. 否则,使用从给定URI获取的查询和片段组件以及通过从给定URI路径的开始处移除该URI的路径来计算路径组件来构建新的相对等级URI。

Parameters
uri URI: The URI to be relativized against this URI
Returns
URI The resulting URI
Throws
NullPointerException If uri is null

resolve

Added in API level 1
URI resolve (URI uri)

根据此URI解析给定的URI。

如果给定的URI已经是绝对的,或者这个URI是不透明的,那么返回给定的URI。

如果定义了给定URI的片段组件,其路径组件是空的,并且其方案,权限和查询组件未定义,则返回具有给定片段但具有与此URI的所有其他组件相同的URI的URI。 这允许表示独立片段引用的URI(例如"#foo")针对基本URI进行有用的解析。

否则,该方法以与RFC 2396的第5.2节一致的方式构造新的分层URI; 那是:

  1. 使用此URI的方案和给定URI的查询和片段组件构建新的URI。

  2. 如果给定的URI具有授权组件,则新的URI的权限和路径将从给定的URI中获取。

  3. 否则,新URI的权限组件将从此URI复制,其路径计算如下:

    1. 如果给定的URI路径是绝对的,那么新URI的路径是从给定的URI中获取的。

    2. 否则,给定URI的路径是相对的,所以通过根据此URI的路径解析给定URI的路径来计算新URI的路径。 这是通过连接这个URI路径的最后一部分(如果有的话)和给定的URI路径,然后像调用normalize方法一样标准化结果来normalize

当且仅当这个URI是绝对的或给定的URI是绝对的时,这个方法的结果才是绝对的。

Parameters
uri URI: The URI to be resolved against this URI
Returns
URI The resulting URI
Throws
NullPointerException If uri is null

resolve

Added in API level 1
URI resolve (String str)

通过解析给定的字符串构造一个新的URI,然后根据这个URI解析它。

这种方便的方法就像调用它相当于评估表达式 resolve(URI.create(str))一样

Parameters
str String: The string to be parsed into a URI
Returns
URI The resulting URI
Throws
NullPointerException If str is null
IllegalArgumentException If the given string violates RFC 2396

toASCIIString

Added in API level 1
String toASCIIString ()

以US-ASCII字符串形式返回此URI的内容。

如果此URI在其他类别中不包含任何字符,则此方法的调用将返回与调用toString方法相同的值。 否则,此方法的工作方式类似于调用该方法,然后encoding结果。

Returns
String The string form of this URI, encoded as needed so that it only contains characters in the US-ASCII charset

toString

Added in API level 1
String toString ()

以字符串形式返回此URI的内容。

如果此URI是通过调用此类中的某个构造函数创建的,则会返回与原始输入字符串等效的字符串或根据最初给定的组件计算得出的字符串。 否则,此URI由标准化,解析或相对化创建,因此根据RFC 2396第5.2节第7步中指定的规则从此URI的组件构建字符串。

Returns
String The string form of this URI

toURL

Added in API level 1
URL toURL ()

从这个URI构造一个URL。

这个便捷方法的工作原理就好像调用它相当于在首先检查这个URI是绝对的后评估表达式 new URL(this.toString())

Returns
URL A URL constructed from this URI
Throws
IllegalArgumentException If this URL is not absolute
MalformedURLException If a protocol handler for the URL could not be found, or if some other error occurred while constructing the URL

Hooray!