public class UriUtils extends Object
| Modifier and Type | Class and Description |
|---|---|
static class |
UriUtils.Type
Enumeration used to identify the allowed characters per URI component.
|
| Constructor and Description |
|---|
UriUtils() |
| Modifier and Type | Method and Description |
|---|---|
static String |
complateUrl(String baseUrl,
String relativePath)
补全相对路径
|
static String |
decode(String source,
Charset charset)
Decode the given encoded URI component.
|
static String |
decode(String source,
String encoding)
Decode the given encoded URI component.
|
static String |
decodeMap(Map<String,?> paramMap,
Charset charset)
将Map形式的Form表单数据转换为Url参数形式
paramMap中如果key为空(null和"")会被忽略,如果value为null,会被做为空白符("") 会自动url编码键和值 |
static Map<String,List<String>> |
decodeObj(String params,
String charset)
将URL参数解析为Map(也可以解析Post中的键值对参数)
|
static Map<String,String> |
decodeVal(String params,
String charset)
将URL参数解析为Map(也可以解析Post中的键值对参数)
|
static String |
encode(String url)
编码URL,默认使用UTF-8编码
将需要转换的内容(ASCII码形式之外的内容),用十六进制表示法转换出来,并在之前加上%开头。
|
static String |
encode(String source,
Charset charset)
Encode all characters that are either illegal, or have any reserved
meaning, anywhere within a URI, as defined in
RFC 3986.
|
static String |
encode(String source,
String encoding)
Variant of
decode(String, Charset) with a String charset. |
static String |
encodeAll(String url)
编码URL,默认使用UTF-8编码
将需要转换的内容(ASCII码形式之外的内容),用十六进制表示法转换出来,并在之前加上%开头。
|
static String |
encodeAll(String url,
Charset charset)
编码URL
将需要转换的内容(ASCII码形式之外的内容),用十六进制表示法转换出来,并在之前加上%开头。
|
static String |
encodeAuthority(String authority,
Charset charset)
Encode the given URI authority with the given encoding.
|
static String |
encodeAuthority(String authority,
String encoding)
Encode the given URI authority with the given encoding.
|
static String |
encodeFragment(String fragment,
Charset charset)
Encode the given URI fragment with the given encoding.
|
static String |
encodeFragment(String fragment,
String encoding)
Encode the given URI fragment with the given encoding.
|
static String |
encodeHost(String host,
Charset charset)
Encode the given URI host with the given encoding.
|
static String |
encodeHost(String host,
String encoding)
Encode the given URI host with the given encoding.
|
static String |
encodePath(String path,
Charset charset)
Encode the given URI path with the given encoding.
|
static String |
encodePath(String path,
String encoding)
Encode the given URI path with the given encoding.
|
static String |
encodePathSegment(String segment,
Charset charset)
Encode the given URI path segment with the given encoding.
|
static String |
encodePathSegment(String segment,
String encoding)
Encode the given URI path segment with the given encoding.
|
static String |
encodePort(String port,
Charset charset)
Encode the given URI port with the given encoding.
|
static String |
encodePort(String port,
String encoding)
Encode the given URI port with the given encoding.
|
static String |
encodeQuery(String query,
Charset charset)
Encode the given URI query with the given encoding.
|
static String |
encodeQuery(String query,
String encoding)
Encode the given URI query with the given encoding.
|
static String |
encodeQueryParam(String queryParam,
Charset charset)
Encode the given URI query parameter with the given encoding.
|
static String |
encodeQueryParam(String queryParam,
String encoding)
Encode the given URI query parameter with the given encoding.
|
static String |
encodeScheme(String scheme,
Charset charset)
Encode the given URI scheme with the given encoding.
|
static String |
encodeScheme(String scheme,
String encoding)
Encode the given URI scheme with the given encoding.
|
static Map<String,String> |
encodeUriVariables(Map<String,?> uriVariables)
Convenience method to apply
encode(String, Charset) to all
given URI variable values. |
static Object[] |
encodeUriVariables(Object... uriVariables)
Convenience method to apply
encode(String, Charset) to all
given URI variable values. |
static String |
encodeUserInfo(String userInfo,
Charset charset)
Encode the given URI user info with the given encoding.
|
static String |
encodeUserInfo(String userInfo,
String encoding)
Encode the given URI user info with the given encoding.
|
static String |
encodeVal(String paramsStr,
Charset charset)
对URL参数做编码,只编码键和值
提供的值可以是url附带参数,但是不能只是url |
static String |
extractFileExtension(String path)
Extract the file extension from the given URI path.
|
static String |
formatUrl(String url)
格式化URL链接
|
static String |
getDecodedPath(URL url)
从URL对象中获取不被编码的路径Path
对于本地路径,URL对象的getPath方法对于包含中文或空格时会被编码,导致本读路径读取错误。
此方法将URL转为URI后获取路径用于解决路径被编码的问题
|
static JarFile |
getJarFile(URL url)
从URL中获取JarFile
|
static String |
getPath(String uriStr)
获得path部分
|
static BufferedReader |
getReader(URL url,
Charset charset)
获得Reader
|
static InputStream |
getStream(URL url)
从URL中获取流
|
static URL |
getURL(File file)
获得URL,常用于使用绝对路径时的情况
|
static URL |
getURL(String pathBaseClassLoader)
获得URL
|
static URL |
getURL(String path,
Class<?> clazz)
获得URL
|
static URL[] |
getURLs(File... files)
获得URL,常用于使用绝对路径时的情况
|
static boolean |
isFileURL(URL url)
提供的URL是否为文件
文件协议包括"file", "vfsfile" 或 "vfs".
|
static boolean |
isJarFileURL(URL url)
提供的URL是否为Jar文件URL 判断依据为file协议且扩展名为.jar
|
static boolean |
isJarURL(URL url)
提供的URL是否为jar包URL 协议包括: "jar", "zip", "vfszip" 或 "wsjar".
|
static String |
normalize(String url)
标准化URL字符串,包括:
|
static String |
normalize(String paramPart,
Charset charset)
标准化参数字符串,即URL中?后的部分
|
static URI |
toURI(String location)
转字符串为URI
|
static URI |
toURI(URL url)
转URL为URI
|
static URL |
toUrlForHttp(String urlStr)
将URL字符串转换为URL对象,并做必要验证
|
static URL |
toUrlForHttp(String urlStr,
URLStreamHandler handler)
将URL字符串转换为URL对象,并做必要验证
|
static String |
uriDecode(String source,
Charset charset) |
static URL |
url(String url)
通过一个字符串形式的URL地址创建URL对象
|
static URL |
url(String url,
URLStreamHandler handler)
通过一个字符串形式的URL地址创建URL对象
|
static String |
withForm(String url,
Map<String,Object> form,
Charset charset,
boolean isEncodeParams)
将表单数据加到URL中(用于GET表单提交)
表单的键值对会被url编码,但是url中原参数不会被编码 |
static String |
withForm(String url,
String queryString,
Charset charset,
boolean isEncode)
将表单数据字符串加到URL中(用于GET表单提交)
|
public static URL url(String url, URLStreamHandler handler)
url - URLhandler - URLStreamHandlerpublic 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)ResourceUtils.getResource(String)public static URL getURL(String path, Class<?> clazz)
path - 相对给定 class所在的路径clazz - 指定classResourceUtils.getResource(String, Class)public static URL getURL(File file)
file - URL对应的文件对象InstrumentException - MalformedURLExceptionpublic static URL[] getURLs(File... files)
files - URL对应的文件对象InstrumentException - MalformedURLExceptionpublic static String formatUrl(String url)
url - 需要格式化的URLnormalize(String)public static String complateUrl(String baseUrl, String relativePath)
baseUrl - 基准URLrelativePath - 相对URLInstrumentException - MalformedURLExceptionpublic static String encode(String url) throws InstrumentException
url - URLInstrumentException - UnsupportedEncodingExceptionpublic static String encodeAll(String url)
url - URLpublic static String encodeAll(String url, Charset charset)
url - URLcharset - 编码public static String getPath(String uriStr)
uriStr - URI路径InstrumentException - 包装URISyntaxExceptionpublic static String getDecodedPath(URL url)
url - URLpublic static URI toURI(URL url) throws InstrumentException
url - URLInstrumentException - 包装URISyntaxExceptionpublic static URI toURI(String location) throws InstrumentException
location - 字符串路径InstrumentException - 包装URISyntaxExceptionpublic static boolean isFileURL(URL url)
url - URLpublic static boolean isJarURL(URL url)
url - URLpublic static boolean isJarFileURL(URL url)
url - the URL to checkpublic static InputStream getStream(URL url)
url - URLpublic static BufferedReader getReader(URL url, Charset charset)
url - URLcharset - 编码BufferedReaderpublic static JarFile getJarFile(URL url)
url - URLpublic static String normalize(String url)
1. 多个/替换为一个
url - URL字符串public static String encodeScheme(String scheme, String encoding)
scheme - the scheme to be encodedencoding - the character encoding to encode topublic static String encodeScheme(String scheme, Charset charset)
scheme - the scheme to be encodedcharset - the character encoding to encode topublic static String encodeAuthority(String authority, String encoding)
authority - the authority to be encodedencoding - the character encoding to encode topublic static String encodeAuthority(String authority, Charset charset)
authority - the authority to be encodedcharset - the character encoding to encode topublic static String encodeUserInfo(String userInfo, String encoding)
userInfo - the user info to be encodedencoding - the character encoding to encode topublic static String encodeUserInfo(String userInfo, Charset charset)
userInfo - the user info to be encodedcharset - the character encoding to encode topublic static String encodeHost(String host, String encoding)
host - the host to be encodedencoding - the character encoding to encode topublic static String encodeHost(String host, Charset charset)
host - the host to be encodedcharset - the character encoding to encode topublic static String encodePort(String port, String encoding)
port - the port to be encodedencoding - the character encoding to encode topublic static String encodePort(String port, Charset charset)
port - the port to be encodedcharset - the character encoding to encode topublic static String encodePath(String path, String encoding)
path - the path to be encodedencoding - the character encoding to encode topublic static String encodePath(String path, Charset charset)
path - the path to be encodedcharset - the character encoding to encode topublic static String encodePathSegment(String segment, String encoding)
segment - the segment to be encodedencoding - the character encoding to encode topublic static String encodePathSegment(String segment, Charset charset)
segment - the segment to be encodedcharset - the character encoding to encode topublic static String encodeQuery(String query, String encoding)
query - the query to be encodedencoding - the character encoding to encode topublic static String encodeQuery(String query, Charset charset)
query - the query to be encodedcharset - the character encoding to encode topublic static String encodeQueryParam(String queryParam, String encoding)
queryParam - the query parameter to be encodedencoding - the character encoding to encode topublic static String encodeQueryParam(String queryParam, Charset charset)
queryParam - the query parameter to be encodedcharset - the character encoding to encode topublic static String encodeFragment(String fragment, String encoding)
fragment - the fragment to be encodedencoding - the character encoding to encode topublic static String encodeFragment(String fragment, Charset charset)
fragment - the fragment to be encodedcharset - the character encoding to encode topublic static String encode(String source, String encoding)
decode(String, Charset) with a String charset.source - the String to be encodedencoding - the character encoding to encode topublic static String encode(String source, Charset charset)
source - the String to be encodedcharset - the character encoding to encode topublic static Map<String,String> encodeUriVariables(Map<String,?> uriVariables)
encode(String, Charset) to all
given URI variable values.uriVariables - the URI variable values to be encodedpublic static Object[] encodeUriVariables(Object... uriVariables)
encode(String, Charset) to all
given URI variable values.uriVariables - the URI variable values to be encodedpublic static String decode(String source, String encoding)
See uriDecode(String, Charset) for the decoding rules.
source - the encoded Stringencoding - the character encoding to useIllegalArgumentException - when the given source contains invalid encoded sequencesuriDecode(String, Charset),
URLDecoder.decode(String, String)public static String decode(String source, Charset charset)
See uriDecode(String, Charset) for the decoding rules.
source - the encoded Stringcharset - the character encoding to useIllegalArgumentException - when the given source contains invalid encoded sequencesuriDecode(String, Charset),
URLDecoder.decode(String, String)public static String extractFileExtension(String path)
path - the URI path (e.g. "/products/index.html")public static String encodeVal(String paramsStr, Charset charset)
注意,此方法只能标准化整个URL,并不适合于单独编码参数值
paramsStr - url参数,可以包含url本身charset - 编码public static String normalize(String paramPart, Charset charset)
注意,此方法只能标准化整个URL,并不适合于单独编码参数值
paramPart - 参数字符串charset - 编码public static String decodeMap(Map<String,?> paramMap, Charset charset)
key1=v1&key2=&key3=v3
paramMap - 表单数据charset - 编码public static Map<String,String> decodeVal(String params, String charset)
params - 参数字符串(或者带参数的Path)charset - 字符集public static Map<String,List<String>> decodeObj(String params, String charset)
params - 参数字符串(或者带参数的Path)charset - 字符集public static String withForm(String url, Map<String,Object> form, Charset charset, boolean isEncodeParams)
url - URLform - 表单数据charset - 编码isEncodeParams - 是否对键和值做转义处理Copyright © 2019. All rights reserved.