Class UrlUtil

java.lang.Object
com.sure.tool.util.UrlUtil

public class UrlUtil extends Object
URL 工具类:编码/解码、解析、参数提取、拼接,零依赖。

English: URL parsing, encoding and query-parameter helpers.

Since:
0.2.0
Author:
suretool
  • Field Details

    • DEFAULT_CHARSET

      public static final Charset DEFAULT_CHARSET
      默认字符集 UTF-8。
  • Method Details

    • encode

      public static String encode(String url)
      URL 编码(空格编码为 +,表单风格)。

      示例:UrlUtil.encode("a b") -> "a+b"。

      Parameters:
      url - URL 或参数值
      Returns:
      编码结果
    • encode

      public static String encode(String url, Charset charset)
      URL 编码(表单风格,空格→+)。

      示例:UrlUtil.encode("你好", CharsetUtil.UTF_8) -> "%E4%BD%A0%E5%A5%BD"。

      Parameters:
      url - URL 或参数值
      charset - 字符集
      Returns:
      编码结果
    • encodePath

      public static String encodePath(String path)
      URL 编码路径片段(空格→%20)。

      示例:UrlUtil.encodePath("a b/c") -> "a%20b%2Fc"。

      Parameters:
      path - 路径
      Returns:
      编码结果
    • decode

      public static String decode(String url)
      URL 解码。

      示例:UrlUtil.decode("a+b") -> "a b"。

      Parameters:
      url - URL 或参数值
      Returns:
      解码结果
    • decode

      public static String decode(String url, Charset charset)
      URL 解码。

      示例:UrlUtil.decode("%E4%BD%A0", CharsetUtil.UTF_8) -> "你"。

      Parameters:
      url - URL 或参数值
      charset - 字符集
      Returns:
      解码结果
    • toUri

      public static URI toUri(String url)
      解析 URL 为 URI(非法返回 null)。

      示例:UrlUtil.toUri("https://example.com/a?b=1") 返回 URI("https://example.com/a?b=1")。

      Parameters:
      url - URL
      Returns:
      URI;解析失败返回 null
    • getHost

      public static String getHost(String url)
      获取主机。

      示例:UrlUtil.getHost("https://example.com:8080/a") -> "example.com"。

      Parameters:
      url - URL
      Returns:
      主机名
    • getPort

      public static int getPort(String url)
      获取端口(未指定时返回 -1)。

      示例:UrlUtil.getPort("https://example.com:8080/a") -> 8080; UrlUtil.getPort("https://example.com/a") -> -1。

      Parameters:
      url - URL
      Returns:
      端口
    • getScheme

      public static String getScheme(String url)
      获取协议(scheme)。

      示例:UrlUtil.getScheme("https://example.com") -> "https"。

      Parameters:
      url - URL
      Returns:
      协议
    • getPath

      public static String getPath(String url)
      获取路径。

      示例:UrlUtil.getPath("https://example.com/a/b?x=1") -> "/a/b"。

      Parameters:
      url - URL
      Returns:
      路径
    • isHttp

      public static boolean isHttp(String url)
      是否为 HTTP 或 HTTPS 协议。

      示例:UrlUtil.isHttp("https://example.com") -> true; UrlUtil.isHttp("ftp://example.com") -> false。

      Parameters:
      url - URL
      Returns:
      是否 http(s)
    • getParams

      public static Map<String,String> getParams(String url)
      获取查询参数。

      示例:UrlUtil.getParams("https://example.com?a=1&b=%E4%BD%A0") -> {"a":"1", "b":"你"}。

      Parameters:
      url - URL
      Returns:
      参数 Map(保持出现顺序)
    • getParam

      public static String getParam(String url, String name)
      获取单个查询参数。

      示例:UrlUtil.getParam("https://example.com?a=1", "a") -> "1"; UrlUtil.getParam("https://example.com", "a") -> null。

      Parameters:
      url - URL
      name - 参数名
      Returns:
      参数值;不存在返回 null
    • appendParams

      public static String appendParams(String url, Map<String,String> params)
      拼接查询参数到 URL。

      示例:UrlUtil.appendParams("https://example.com/a", {"b":"2"}) -> "https://example.com/a?b=2"。

      Parameters:
      url - 基础 URL(可含已有 ?query)
      params - 参数
      Returns:
      拼接后的 URL
    • normalize

      public static String normalize(String url)
      URL 规范化:缺协议补全 http://、去除末尾斜杠(保留根路径)。

      示例:UrlUtil.normalize("example.com/a/") -> "http://example.com/a"; UrlUtil.normalize("https://example.com") -> "https://example.com"。

      Parameters:
      url - URL
      Returns:
      规范化后的 URL;null 返回 null
    • isHttps

      public static boolean isHttps(String url)
      判断是否为 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

      public static boolean isValidUrl(String url)
      校验是否为合法 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

      public static URL toUrl(String url)
      转为 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

      public static String getDomain(String url)
      提取主机域名(去协议、端口、路径与查询参数)。

      示例:UrlUtil.getDomain("https://www.example.com:8080/path?a=1") -> "www.example.com"。

      Parameters:
      url - URL,可为 null
      Returns:
      域名;null 或无法解析返回 null
      Since:
      1.19.0
    • toHttpUrl

      public static String toHttpUrl(String url)
      无协议时补全 http://(有协议或不可识别时原样返回)。

      示例:UrlUtil.toHttpUrl("example.com/a") -> "http://example.com/a"。

      Parameters:
      url - URL,可为 null
      Returns:
      补全后的 URL;null 返回 null
      Since:
      1.19.0
    • removeProtocol

      public static String removeProtocol(String url)
      剥离协议前缀。

      示例:UrlUtil.removeProtocol("https://example.com/a") -> "example.com/a"。

      Parameters:
      url - URL,可为 null
      Returns:
      去协议后的字符串;null 返回 null
      Since:
      1.19.0
    • normalizePath

      public static String normalizePath(String path)
      路径规范化:解析 . 与 ..、合并连续斜杠(RFC 3986 §5.2.4)。

      示例:UrlUtil.normalizePath("/a/./b") -> "/a/b"; UrlUtil.normalizePath("/a/../b") -> "/b"。

      Parameters:
      path - 路径(含或不含前导斜杠),可为 null
      Returns:
      规范化后的路径;null 返回 null
      Since:
      1.19.0
    • isSameOrigin

      public static boolean isSameOrigin(String url1, String url2)
      判断两个 URL 是否同源(协议 + 主机 + 端口一致,忽略默认端口差异)。

      示例:UrlUtil.isSameOrigin("https://a.com/x", "https://a.com/y") -> true; UrlUtil.isSameOrigin("https://a.com", "https://b.com") -> false。

      Parameters:
      url1 - URL1,可为 null
      url2 - URL2,可为 null
      Returns:
      是否同源;任一无法解析返回 false
      Since:
      1.19.0
    • getQuery

      public static String getQuery(String url)
      提取查询串(不含 ?;无查询返回空串)。

      示例:UrlUtil.getQuery("https://example.com/a?b=1&c=2") -> "b=1&c=2"。

      Parameters:
      url - URL,可为 null
      Returns:
      查询串;null 返回 null
      Since:
      1.19.0
    • getFragment

      public static String getFragment(String url)
      提取锚点 fragment(不含 #;无锚点返回空串)。

      示例:UrlUtil.getFragment("https://example.com/a#section2") -> "section2"。

      Parameters:
      url - URL,可为 null
      Returns:
      锚点;null 返回 null
      Since:
      1.19.0