public class UrlUtil extends Object
统一资源定位符,描述了一台特定服务器上某资源的特定位置。
URL组成:协议://主机名[:端口]/ 路径/[:参数] [?查询]#Fragment protocol :// hostname[:port] / path / [:parameters][?query]#fragment
| Modifier and Type | Field and Description |
|---|---|
static String |
CLASSPATH_URL_PREFIX
针对ClassPath路径的伪协议前缀(兼容Spring): "classpath:"
|
static String |
FILE_URL_PREFIX
URL 前缀表示文件: "file:"
|
static String |
JAR_URL_PREFIX
URL 前缀表示jar: "jar:"
|
static String |
JAR_URL_SEPARATOR
Jar路径以及内部文件路径的分界符: "!
|
static String |
WAR_URL_PREFIX
URL 前缀表示war: "war:"
|
static String |
WAR_URL_SEPARATOR
WAR路径及内部文件路径分界符
|
| Constructor and Description |
|---|
UrlUtil() |
| Modifier and Type | Method and Description |
|---|---|
static String |
buildQuery(Map<String,?> paramMap,
Charset charset)
将Map形式的Form表单数据转换为Url参数形式
paramMap中如果key为空(null和"")会被忽略,如果value为null,会被做为空白符("") 会自动url编码键和值 key1=v1&key2=&key3=v3 |
static String |
completeUrl(String baseUrl,
String relativePath)
补全相对路径
|
static String |
getDataUri(String mimeType,
Charset charset,
String encoding,
String data)
Data URI Scheme封装。
|
static String |
getDataUri(String mimeType,
String encoding,
String data)
Data URI Scheme封装。
|
static String |
getDataUriBase64(String mimeType,
String data)
Data URI Scheme封装,数据格式为Base64。
|
static String |
getDecodedPath(URL url)
从URL对象中获取不被编码的路径Path
对于本地路径,URL对象的getPath方法对于包含中文或空格时会被编码,导致本读路径读取错误。 |
static URI |
getHost(URL url)
获取URL中域名部分,只保留URL中的协议(Protocol)、Host,其它为null。
|
static String |
getPath(String uriStr)
获得path部分
|
static BufferedReader |
getReader(URL url,
Charset charset)
获得Reader
|
static InputStream |
getStream(URL url)
从URL中获取流
|
static URI |
getStringURI(CharSequence content)
获取string协议的URL,类似于string:///xxxxx
|
static URL |
getURL(File file)
获得URL,常用于使用绝对路径时的情况
|
static URL |
getURL(String pathBaseClassLoader)
获得URL
|
static URL |
getURL(String path,
Class<?> clazz)
获得URL
|
static URL |
getURL(URL url,
String relativePath)
获取相对于给定URL的新的URL
来自:org.springframework.core.io.UrlResource#createRelativeURL |
static URL[] |
getURLs(File... files)
获得URL,常用于使用绝对路径时的情况
|
static String |
normalize(String url)
标准化URL字符串,包括:
自动补齐“http://”头
去除开头的\或者/
替换\为/
|
static String |
normalize(String url,
boolean isEncodePath)
标准化URL字符串,包括:
自动补齐“http://”头
去除开头的\或者/
替换\为/
|
static String |
normalize(String url,
boolean isEncodePath,
boolean replaceSlash)
标准化URL字符串,包括:
自动补齐“http://”头
去除开头的\或者/
替换\为/
如果replaceSlash为true,则替换多个/为一个
|
static long |
size(URL url)
获取URL对应数据长度
如果URL为文件,转换为文件获取文件长度。
|
static URI |
toURI(String location)
转字符串为URI
|
static URI |
toURI(String location,
boolean isEncode)
转字符串为URI
|
static URI |
toURI(URL url)
转URL为URI
|
static URI |
toURI(URL url,
boolean isEncode)
转URL为URI
|
static URL |
toUrlForHttp(String urlStr)
将URL字符串转换为URL对象,并做必要验证
|
static URL |
toUrlForHttp(String urlStr,
URLStreamHandler handler)
将URL字符串转换为URL对象,并做必要验证
|
static URL |
url(String url)
通过一个字符串形式的URL地址创建URL对象
|
static URL |
url(String url,
URLStreamHandler handler)
通过一个字符串形式的URL地址创建URL对象
|
static URL |
url(URI uri)
|
static void |
useCachesIfNecessary(URLConnection con)
如果连接为JNLP方式,则打开缓存
|
public static final String CLASSPATH_URL_PREFIX
public static final String FILE_URL_PREFIX
public static final String JAR_URL_PREFIX
public static final String WAR_URL_PREFIX
public static final String JAR_URL_SEPARATOR
public static final String WAR_URL_SEPARATOR
public static URL url(URI uri) throws HutoolException
uri - URIHutoolException - MalformedURLException包装,URI格式有问题时抛出URI.toURL()public static URL url(String url, URLStreamHandler handler)
url - URLhandler - URLStreamHandlerpublic static URI getStringURI(CharSequence content)
content - 正文public static URL toUrlForHttp(String urlStr)
urlStr - URL字符串public static URL toUrlForHttp(String urlStr, URLStreamHandler handler)
urlStr - URL字符串handler - URLStreamHandlerpublic static URL getURL(String pathBaseClassLoader)
pathBaseClassLoader - 相对路径(相对于classes)ResourceUtil.getResourceUrl(String)public static URL getURL(String path, Class<?> clazz)
path - 相对给定 class所在的路径clazz - 指定classResourceUtil.getResourceUrl(String, Class)public static URL getURL(File file)
file - URL对应的文件对象IORuntimeException - URL格式错误public static URL getURL(URL url, String relativePath) throws HutoolException
url - 基础URLrelativePath - 相对路径IORuntimeException - URL格式错误HutoolExceptionpublic static URL[] getURLs(File... files)
files - URL对应的文件对象IORuntimeException - URL格式错误public static URI getHost(URL url)
url - URLpublic static String completeUrl(String baseUrl, String relativePath)
baseUrl - 基准URLrelativePath - 相对URLHutoolException - MalformedURLExceptionpublic static String getPath(String uriStr)
uriStr - URI路径HutoolException - 包装URISyntaxExceptionpublic static String getDecodedPath(URL url)
url - URLpublic static URI toURI(URL url) throws HutoolException
url - URLHutoolException - 包装URISyntaxExceptionpublic static URI toURI(URL url, boolean isEncode) throws HutoolException
url - URLisEncode - 是否编码参数中的特殊字符(默认UTF-8编码)HutoolException - 包装URISyntaxExceptionpublic static URI toURI(String location) throws HutoolException
location - 字符串路径HutoolException - 包装URISyntaxExceptionpublic static URI toURI(String location, boolean isEncode) throws HutoolException
location - 字符串路径isEncode - 是否编码参数中的特殊字符(默认UTF-8编码)HutoolException - 包装URISyntaxExceptionpublic static InputStream getStream(URL url)
url - URLpublic static BufferedReader getReader(URL url, Charset charset)
url - URLcharset - 编码BufferedReaderpublic static String normalize(String url)
url - URL字符串public static String normalize(String url, boolean isEncodePath)
url - URL字符串isEncodePath - 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分)public static String normalize(String url, boolean isEncodePath, boolean replaceSlash)
url - URL字符串isEncodePath - 是否对URL中path部分的中文和特殊字符做转义(不包括 http:, /和域名部分)replaceSlash - 是否替换url body中的 //public static String buildQuery(Map<String,?> paramMap, Charset charset)
key1=v1&key2=&key3=v3
paramMap - 表单数据charset - 编码,编码为null表示不编码public static String getDataUriBase64(String mimeType, String data)
Data URI的格式规范:
data:[<mime type>][;charset=<charset>][;<encoding>],<encoded data>
mimeType - 可选项(null表示无),数据类型(image/png、text/plain等)data - 编码后的数据public static String getDataUri(String mimeType, String encoding, String data)
Data URI的格式规范:
data:[<mime type>][;charset=<charset>][;<encoding>],<encoded data>
mimeType - 可选项(null表示无),数据类型(image/png、text/plain等)encoding - 数据编码方式(US-ASCII,BASE64等)data - 编码后的数据public static String getDataUri(String mimeType, Charset charset, String encoding, String data)
Data URI的格式规范:
data:[<mime type>][;charset=<charset>][;<encoding>],<encoded data>
mimeType - 可选项(null表示无),数据类型(image/png、text/plain等)charset - 可选项(null表示无),源文本的字符集编码方式encoding - 数据编码方式(US-ASCII,BASE64等)data - 编码后的数据public static long size(URL url)
URLConnection.getContentLengthLong()url - URLpublic static void useCachesIfNecessary(URLConnection con)
con - URLConnectionCopyright © 2025. All rights reserved.