20230627 java.net.URI
介绍
- java.net.URI
- public final class URI implements Comparable
, Serializable - URI 是个纯粹的语法结构,包含用来指定 Web 资源的字符串的各种组成部分
- URL 是 URI 的一个特例,它包含了用于定位 Web 资源的足够信息
URI 语法
URI 具有以下句法:
[scheme:]schemeSpecficPart[#fragment]
一个分层 URI schemeSpecificPart
具有以下结构:
[//authority][path][?query]
对于那些基于服务器的 URI , authority
部分具有以下形式:
[user-info@]host[:port]
API
static
- create
构造器
- URI(String str) throws URISyntaxException
- URI(String scheme, String userInfo, String host, int port, String path, String query, String fragment)
- URI(String scheme, String authority, String path, String query, String fragment) throws URISyntaxException
- URI(String scheme, String host, String path, String fragment) throws URISyntaxException
- URI(String scheme, String ssp, String fragment) throws URISyntaxException
public
-
resolve
- 解析相对化
-
relativize
- 反
resolve
- 相对化
- 反
-
normalize
- 正常化
-
isAbsolute
-
isOpaque
- 是否不透明的
- 当绝对 URI 的 schemeSpecficPart 不是以 / 开头的
-
toASCIIString
- 以 US-ASCII 字符串形式返回此 URI 的内容
- 会进行转码
-
toURL
- 转
URL
- 转
-
parseServerAuthority
- 尝试将此 URI 的 authority 解析为 user-info, host, port
URI 句法:
- getScheme
- getRawSchemeSpecificPart, getSchemeSpecificPart
- 方法名不带 raw 表示解码后的
- getRawFragment, getFragment
分层 SchemeSpecificPart 结构:
- getRawAuthority, getAuthority
- getRawPath, getPath
- getRawQuery, getQuery
authority 结构:
- getRawUserInfo, getUserInfo
- getHost
- getPort
- 返回 -1 表示未定义
示例代码
import cn.hutool.core.lang.Console;
import java.net.URI;
public class TestURI {
public static void main(String[] args) {
URI uri = URI.create(
"https://www.bilibili.com/video/BV1Rj411D7Eg/?spm_id_from=333.1007.tianma.1-1-1"
+ ".click&vd_source=4a4c5fa550920bace1f35eeab12544f0");
Console.log(uri);
Console.log("Scheme :: ", uri.getScheme());
Console.log("SchemeSpecificPart :: ", uri.getSchemeSpecificPart());
Console.log("Fragment :: ", uri.getFragment());
Console.log("Authority :: ", uri.getAuthority());
Console.log("Path :: ", uri.getPath());
Console.log("Query :: ", uri.getQuery());
Console.log("UserInfo :: ", uri.getUserInfo());
Console.log("Host :: ", uri.getHost());
Console.log("Port :: ", uri.getPort());
}
}
打印结果
https://www.bilibili.com/video/BV1Rj411D7Eg/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=4a4c5fa550920bace1f35eeab12544f0
Scheme :: https
SchemeSpecificPart :: //www.bilibili.com/video/BV1Rj411D7Eg/?spm_id_from=333.1007.tianma.1-1-1.click&vd_source=4a4c5fa550920bace1f35eeab12544f0
Fragment :: null
Authority :: www.bilibili.com
Path :: /video/BV1Rj411D7Eg/
Query :: spm_id_from=333.1007.tianma.1-1-1.click&vd_source=4a4c5fa550920bace1f35eeab12544f0
UserInfo :: null
Host :: www.bilibili.com
Port :: -1