Class UrlUtil
java.lang.Object
com.sure.tool.util.UrlUtil
URL 工具类:编码/解码、解析、参数提取、拼接,零依赖。
English: URL parsing, encoding and query-parameter helpers.
- Since:
- 0.2.0
- Author:
- suretool
-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionstatic StringappendParams(String url, Map<String, String> params) 拼接查询参数到 URL。static StringURL 解码。static StringURL 解码。static StringURL 编码(空格编码为 +,表单风格)。static StringURL 编码(表单风格,空格→+)。static StringencodePath(String path) URL 编码路径片段(空格→%20)。static String提取主机域名(去协议、端口、路径与查询参数)。static StringgetFragment(String url) 提取锚点 fragment(不含#;无锚点返回空串)。static String获取主机。static String获取单个查询参数。获取查询参数。static String获取路径。static int获取端口(未指定时返回 -1)。static String提取查询串(不含?;无查询返回空串)。static String获取协议(scheme)。static boolean是否为 HTTP 或 HTTPS 协议。static boolean判断是否为 https 协议。static booleanisSameOrigin(String url1, String url2) 判断两个 URL 是否同源(协议 + 主机 + 端口一致,忽略默认端口差异)。static booleanisValidUrl(String url) 校验是否为合法 http/https URL。static StringURL 规范化:缺协议补全http://、去除末尾斜杠(保留根路径)。static StringnormalizePath(String path) 路径规范化:解析.与..、合并连续斜杠(RFC 3986 §5.2.4)。static StringremoveProtocol(String url) 剥离协议前缀。static String无协议时补全http://(有协议或不可识别时原样返回)。static URI解析 URL 为 URI(非法返回 null)。static URL
-
Field Details
-
DEFAULT_CHARSET
默认字符集 UTF-8。
-
-
Method Details
-
encode
-
encode
-
encodePath
-
decode
-
decode
-
toUri
-
getHost
-
getPort
获取端口(未指定时返回 -1)。示例:
UrlUtil.getPort("https://example.com:8080/a") -> 8080;UrlUtil.getPort("https://example.com/a") -> -1。- Parameters:
url- URL- Returns:
- 端口
-
getScheme
-
getPath
-
isHttp
是否为 HTTP 或 HTTPS 协议。示例:
UrlUtil.isHttp("https://example.com") -> true;UrlUtil.isHttp("ftp://example.com") -> false。- Parameters:
url- URL- Returns:
- 是否 http(s)
-
getParams
-
getParam
-
appendParams
-
normalize
-
isHttps
判断是否为 https 协议。示例:
UrlUtil.isHttps("https://example.com") -> true;UrlUtil.isHttps("http://example.com") -> false。- Parameters:
url- URL,可为null- Returns:
- 是否 https;
null或无法解析返回false - Since:
- 1.19.0
-
isValidUrl
校验是否为合法 http/https URL。示例:
UrlUtil.isValidUrl("https://example.com/a?b=1") -> true;UrlUtil.isValidUrl("not a url") -> false。- Parameters:
url- URL,可为null- Returns:
- 是否合法;
null返回false - Since:
- 1.19.0
-
toUrl
转为URL对象;非法 URL 抛RuntimeException(包装MalformedURLException)。示例:
UrlUtil.toUrl("https://example.com")返回URL对象。- Parameters:
url- URL 字符串,可为null- Returns:
- URL 对象;
null返回null - Throws:
RuntimeException- 非法 URL- Since:
- 1.19.0
-
getDomain
-
toHttpUrl
-
removeProtocol
-
normalizePath
-
isSameOrigin
判断两个 URL 是否同源(协议 + 主机 + 端口一致,忽略默认端口差异)。示例:
UrlUtil.isSameOrigin("https://a.com/x", "https://a.com/y") -> true;UrlUtil.isSameOrigin("https://a.com", "https://b.com") -> false。- Parameters:
url1- URL1,可为nullurl2- URL2,可为null- Returns:
- 是否同源;任一无法解析返回
false - Since:
- 1.19.0
-
getQuery
-
getFragment
-