Java如何解析url,包括自定义schema的url

比如我要解析下面这个url:

myscheme://admin@blog.nihao001.com:8080/index.jsp?username=asdf&password=123456&msg=%e4%bd%a0%e5%a5%bd#editor

只要使用URI这个类就可以解析了。

上代码:

import java.net.URI;
import java.net.URISyntaxException;

public class Main {

	public static void main(String[] args) {
		String SCHEMA ="myscheme://admin@blog.nihao001.com:8080/index.jsp?username=asdf&password=123456&msg=%e4%bd%a0%e5%a5%bd#editor";
		try {
			URI uri = new URI(SCHEMA);
			System.out.println("getScheme:" + uri.getScheme());
			System.out.println("getHost:" + uri.getHost());
			System.out.println("getPort:" + uri.getPort());
			System.out.println("getAuthority:" + uri.getAuthority());
			System.out.println("getPath:" + uri.getPath());
			System.out.println("getRawQuery:" + uri.getRawQuery());
			System.out.println("getQuery:" + uri.getQuery());
			System.out.println("getFragment:" + uri.getFragment());
			System.out.println("getRawUserInfo:" + uri.getRawUserInfo());
			System.out.println("getUserInfo:" + uri.getUserInfo());
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
	}

}

看结果:

getScheme:myscheme
getHost:blog.nihao001.com
getPort:8080
getAuthority:admin@blog.nihao001.com:8080
getPath:/index.jsp
getRawQueryusername=asdf&password=123456&msg=%e4%bd%a0%e5%a5%bd
getQuery:username=asdf&password=123456&msg=你好
getFragment:editor
getRawUserInfo:admin
getUserInfo:admin
此条目发表在IT分类目录,贴了, , , , 标签。将固定链接加入收藏夹。

发表评论

电子邮件地址不会被公开。

*