Package org.miaixz.bus.core.xyz
Class UrlKit
java.lang.Object
org.miaixz.bus.core.xyz.UrlKit
URL(Uniform Resource Locator)统一资源定位符相关工具类
统一资源定位符,描述了一台特定服务器上某资源的特定位置。
URL组成:协议://主机名[:端口]/ 路径/[:参数] [?查询]#Fragment protocol :// hostname[:port] / path / [:parameters][?query]#fragment
- Since:
- Java 17+
- Author:
- Kimi Liu
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StringbuildQuery(Map<String, ?> paramMap, Charset charset) 将Map形式的Form表单数据转换为Url参数形式 paramMap中如果key为空(null和"")会被忽略,如果value为null,会被做为空白符("") 会自动url编码键和值static StringcompleteUrl(String baseUrl, String relativePath) 补全相对路径decodeQuery(String paramsStr, Charset charset) 将URL参数解析为Map(也可以解析Post中的键值对参数)decodeQueryList(String paramsStr, Charset charset) 将URL参数解析为Map(也可以解析Post中的键值对参数)static StringencodeQuery(String urlWithParams, Charset charset) 对URL参数做编码,只编码键和值 提供的值可以是url附带参数,但是不能只是urlstatic StringgetDataUri(String mimeType, String encoding, String data) Data URI Scheme封装。static StringgetDataUri(String mimeType, Charset charset, String encoding, String data) Data URI Scheme封装。static StringgetDataUriBase64(String mimeType, String data) Data URI Scheme封装,数据格式为Base64。static StringgetDecodedPath(URL url) 从URL对象中获取不被编码的路径Path 对于本地路径,URL对象的getPath方法对于包含中文或空格时会被编码,导致本读路径读取错误。static URI获取URL中域名部分,只保留URL中的协议(Protocol)、Host,其它为null。static String获得path部分static BufferedReader获得Readerstatic InputStream从URL中获取流static URIgetStringURI(CharSequence content) 获取string协议的URL,类似于string:///xxxxxstatic URL获得URL,常用于使用绝对路径时的情况static URL获得URLstatic URL获得URLstatic URL获取相对于给定URL的新的URL 来自:org.springframework.core.io.UrlResource#createRelativeURLstatic URL[]获得URL,常用于使用绝对路径时的情况static String标准化URL字符串,包括: 自动补齐“http://”头 去除开头的\或者/ 替换\为/static String标准化URL字符串,包括: 自动补齐“http://”头 去除开头的\或者/ 替换\为/static String标准化URL字符串,包括: 自动补齐“http://”头 去除开头的\或者/ 替换\为/ 如果replaceSlash为true,则替换多个/为一个static StringnormalizeQuery(String queryPart, Charset charset) 标准化参数字符串,即URL中?static long获取URL对应数据长度 如果URL为文件,转换为文件获取文件长度。static String将Map形式的Form表单数据转换为Url参数形式,会自动url编码键和值static String将Map形式的Form表单数据转换为Url参数形式 paramMap中如果key为空(null和"")会被忽略,如果value为null,会被做为空白符("") 会自动url编码键和值 此方法用于拼接URL中的Query部分,并不适用于POST请求中的表单static StringtoQuery(Map<String, ?> paramMap, Charset charset, UrlQuery.EncodeMode encodeMode) 将Map形式的Form表单数据转换为Url参数形式 paramMap中如果key为空(null和"")会被忽略,如果value为null,会被做为空白符("") 会自动url编码键和值static URI转字符串为URIstatic URI转字符串为URIstatic URI转URL为URIstatic URI转URL为URIstatic URLtoUrlForHttp(String urlStr) 将URL字符串转换为URL对象,并做必要验证static URLtoUrlForHttp(String urlStr, URLStreamHandler handler) 将URL字符串转换为URL对象,并做必要验证static URL通过一个字符串形式的URL地址创建URL对象static URLurl(String url, URLStreamHandler handler) 通过一个字符串形式的URL地址创建URL对象static URLstatic void如果连接为JNLP方式,则打开缓存
-
Constructor Details
-
UrlKit
public UrlKit()
-
-
Method Details
-
url
- Parameters:
uri-URI- Returns:
- URL对象
- Throws:
InternalException-MalformedURLException包装,URI格式有问题时抛出- See Also:
-
url
-
url
通过一个字符串形式的URL地址创建URL对象- Parameters:
url- URLhandler-URLStreamHandler- Returns:
- URL对象
-
getStringURI
获取string协议的URL,类似于string:///xxxxx- Parameters:
content- 正文- Returns:
- URL
-
toUrlForHttp
-
toUrlForHttp
将URL字符串转换为URL对象,并做必要验证- Parameters:
urlStr- URL字符串handler-URLStreamHandler- Returns:
- URL
-
getURL
-
getURL
-
getURL
获得URL,常用于使用绝对路径时的情况- Parameters:
file- URL对应的文件对象- Returns:
- URL
- Throws:
InternalException- URL格式错误
-
getURL
获取相对于给定URL的新的URL 来自:org.springframework.core.io.UrlResource#createRelativeURL- Parameters:
url- 基础URLrelativePath- 相对路径- Returns:
- 相对于URL的子路径URL
- Throws:
InternalException- URL格式错误
-
getURLs
获得URL,常用于使用绝对路径时的情况- Parameters:
files- URL对应的文件对象- Returns:
- URL
- Throws:
InternalException- URL格式错误
-
getHost
-
completeUrl
补全相对路径- Parameters:
baseUrl- 基准URLrelativePath- 相对URL- Returns:
- 相对路径
- Throws:
InternalException- MalformedURLException
-
getPath
获得path部分- Parameters:
uriStr- URI路径- Returns:
- path
- Throws:
InternalException- 包装URISyntaxException
-
getDecodedPath
-
toURI
转URL为URI- Parameters:
url- URL- Returns:
- URI
- Throws:
InternalException- 包装URISyntaxException
-
toURI
转URL为URI- Parameters:
url- URLisEncode- 是否编码参数中的特殊字符(默认UTF-8编码)- Returns:
- URI
- Throws:
InternalException- 包装URISyntaxException
-
toURI
转字符串为URI- Parameters:
location- 字符串路径- Returns:
- URI
- Throws:
InternalException- 包装URISyntaxException
-
toURI
转字符串为URI- Parameters:
location- 字符串路径isEncode- 是否编码参数中的特殊字符(默认UTF-8编码)- Returns:
- URI
- Throws:
InternalException- 包装URISyntaxException
-
getStream
-
getReader
获得Reader- Parameters:
url-URLcharset- 编码- Returns:
BufferedReader
-
normalize
-
normalize
-
normalize
-
buildQuery
-
getDataUriBase64
Data URI Scheme封装,数据格式为Base64。data URI scheme 允许我们使用内联(inline-code)的方式在网页中包含数据, 目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入。常用于将图片嵌入网页。 Data URI的格式规范:data:[<mime type>][;charset=<charset>][;<encoding>],<encoded data>- Parameters:
mimeType- 可选项(null表示无),数据类型(image/png、text/plain等)data- 编码后的数据- Returns:
- Data URI字符串
-
getDataUri
Data URI Scheme封装。data URI scheme 允许我们使用内联(inline-code)的方式在网页中包含数据, 目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入。常用于将图片嵌入网页。 Data URI的格式规范:data:[<mime type>][;charset=<charset>][;<encoding>],<encoded data>- Parameters:
mimeType- 可选项(null表示无),数据类型(image/png、text/plain等)encoding- 数据编码方式(US-ASCII,BASE64等)data- 编码后的数据- Returns:
- Data URI字符串
-
getDataUri
Data URI Scheme封装。data URI scheme 允许我们使用内联(inline-code)的方式在网页中包含数据, 目的是将一些小的数据,直接嵌入到网页中,从而不用再从外部文件载入。常用于将图片嵌入网页。 Data URI的格式规范:data:[<mime type>][;charset=<charset>][;<encoding>],<encoded data>- Parameters:
mimeType- 可选项(null表示无),数据类型(image/png、text/plain等)charset- 可选项(null表示无),源文本的字符集编码方式encoding- 数据编码方式(US-ASCII,BASE64等)data- 编码后的数据- Returns:
- Data URI字符串
-
size
获取URL对应数据长度- 如果URL为文件,转换为文件获取文件长度。
- 其它情况获取
URLConnection.getContentLengthLong()
- Parameters:
url- URL- Returns:
- 长度
-
useCachesIfNecessary
如果连接为JNLP方式,则打开缓存- Parameters:
con-URLConnection
-
toQuery
-
toQuery
-
toQuery
public static String toQuery(Map<String, ?> paramMap, Charset charset, UrlQuery.EncodeMode encodeMode) 将Map形式的Form表单数据转换为Url参数形式 paramMap中如果key为空(null和"")会被忽略,如果value为null,会被做为空白符("") 会自动url编码键和值key1=v1&key2=&key3=v3
- Parameters:
paramMap- 表单数据charset- 编码,null表示不encode键值对encodeMode- 编码模式- Returns:
- url参数
-
encodeQuery
-
normalizeQuery
-
decodeQuery
-
decodeQueryList
-