欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

URI

程序员文章站 2024-02-13 20:46:40
...

前言

  • jdk1.8
  • java.net.URI 中关于URI的说明很详细。

URI格式

URI的格式可以概括为:[scheme:]scheme-specific-part[#fragment]
将URI格式细化一些,看起来是这样:[scheme:][//authority][path][?query][#fragment]
将URI格式再细化一些,看起来是这样:[scheme:][//[[email protected]]host[:port]][path][?query][#fragment]

为什么是这样呢?参考下面的文档。
JDK 中 java.net.URI 的介绍
RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax

解析URI示例

public class Test {

	public static void main(String[] args) {
		URI uri = URI.create("http://localhost:8080/admin/index?id=1&random=123321#user_info");
		System.out.println("scheme is : " + uri.getScheme());
		System.out.println("scheme-specific-part is : " + uri.getSchemeSpecificPart());
		System.out.println("fragment is : " + uri.getFragment());
		
		System.out.println("authority is : " + uri.getAuthority());
		System.out.println("path is : " + uri.getPath());
		System.out.println("query is : " + uri.getQuery());
		
		System.out.println("user-info is : " + uri.getUserInfo());
		System.out.println("host is : " + uri.getHost());
		System.out.println("port is : " + uri.getPort());
		
		System.out.println("username is : " + (null==uri.getUserInfo()?"":uri.getUserInfo().split(":", 2)[0]));
		System.out.println("password is : " + (null==uri.getUserInfo()?"":uri.getUserInfo().split(":", 2)[1]));
	}

}

执行结果:

scheme is : http
scheme-specific-part is : //localhost:8080/admin/index?id=1&random=123321
fragment is : user_info
authority is : localhost:8080
path is : /admin/index
query is : id=1&random=123321
user-info is : null
host is : localhost
port is : 8080
username is : 
password is : 

特殊字符转义

  • + 转义后 %2B
  • 空格 转义后 %20
  • / 转义后 %2F
  • ? 转义后 %3F
  • % 转义后 %25
  • # 转义后 %23
  • & 转义后 %26
  • = 转义后 %3D

URI示例

http://localhost:8080/admin/index?id=1&random=123321#user_info
---------------------------------------------------------------------------------
scheme is : http
scheme-specific-part is : //localhost:8080/admin/index?id=1&random=123321
fragment is : user_info
authority is : localhost:8080
path is : /admin/index
query is : id=1&random=123321
user-info is : null
host is : localhost
port is : 8080
username is : 
password is : 
redis://:[email protected]:6379/0
---------------------------------------------------------------------------------
scheme is : redis
scheme-specific-part is : //:[email protected]:6379/0
fragment is : null
authority is : :[email protected]:6379
path is : /0
query is : null
user-info is : :123456
host is : 127.0.0.1
port is : 6379
username is : 
password is : 123456
redis://:nk%[email protected]:6379/0
---------------------------------------------------------------------------------
scheme is : redis
scheme-specific-part is : //:nk#[email protected]:6379/0
fragment is : null
authority is : :nk#[email protected]:6379
path is : /0
query is : null
user-info is : :nk#123456
host is : 127.0.0.1
port is : 6379
username is : 
password is : nk#123456
file:///~/calendar
---------------------------------------------------------------------------------
scheme is : file
scheme-specific-part is : ///~/calendar
fragment is : null
authority is : null
path is : /~/calendar
query is : null
user-info is : null
host is : null
port is : -1
username is : 
password is : 
ftp://test:[email protected]:21/profile
---------------------------------------------------------------------------------
scheme is : ftp
scheme-specific-part is : //test:[email protected]:21/profile
fragment is : null
authority is : test:[email protected]:21
path is : /profile
query is : null
user-info is : test:123456
host is : 192.168.0.1
port is : 21
username is : test
password is : 123456
mailto:[email protected]
---------------------------------------------------------------------------------
scheme is : mailto
scheme-specific-part is : [email protected]
fragment is : null
authority is : null
path is : null
query is : null
user-info is : null
host is : null
port is : -1
username is : 
password is : 
sip:[email protected]
---------------------------------------------------------------------------------
scheme is : sip
scheme-specific-part is : [email protected]
fragment is : null
authority is : null
path is : null
query is : null
user-info is : null
host is : null
port is : -1
username is : 
password is : 
magnet:?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C
---------------------------------------------------------------------------------
scheme is : magnet
scheme-specific-part is : ?xt=urn:sha1:YNCKHTQCWBTRNJIV4WNAE52SJUQCZO5C
fragment is : null
authority is : null
path is : null
query is : null
user-info is : null
host is : null
port is : -1
username is : 
password is : 
wss://zhangsan:[email protected]
---------------------------------------------------------------------------------
scheme is : wss
scheme-specific-part is : //zhangsan:[email protected]
fragment is : null
authority is : zhangsan:[email protected]
path is : 
query is : null
user-info is : zhangsan:123456
host is : example.com
port is : -1
username is : zhangsan
password is : 123456
相关标签: java