类 UriUtils
- java.lang.Object
- org.springframework.web.util.UriUtils
public abstract class UriUtils extends Object
Utility methods for URI encoding and decoding based on RFC 3986.There are two types of encode methods:
"encodeXyz"
-- these encode a specific URI component (e.g. path, query) by percent encoding illegal characters, which includes non-US-ASCII characters, and also characters that are otherwise illegal within the given URI component type, as defined in RFC 3986. The effect of this method, with regards to encoding, is comparable to using the multi-argument constructor ofURI
."encode"
and"encodeUriVariables"
-- these can be used to encode URI variable values by percent encoding all characters that are either illegal, or have any reserved meaning, anywhere within a URI.
- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
- 另请参阅:
- RFC 3986
构造器概要
构造器 构造器 说明 UriUtils()
方法概要
所有方法 静态方法 具体方法 修饰符和类型 方法 说明 static String
decode(String source, String encoding)
Decode the given encoded URI component.static String
decode(String source, Charset charset)
Decode the given encoded URI component.static String
encode(String source, String encoding)
Variant ofencode(String, Charset)
with a String charset.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
encodeAuthority(String authority, String encoding)
Encode the given URI authority with the given encoding.static String
encodeAuthority(String authority, Charset charset)
Encode the given URI authority with the given encoding.static String
encodeFragment(String fragment, String encoding)
Encode the given URI fragment with the given encoding.static String
encodeFragment(String fragment, Charset charset)
Encode the given URI fragment with the given encoding.static String
encodeHost(String host, String encoding)
Encode the given URI host with the given encoding.static String
encodeHost(String host, Charset charset)
Encode the given URI host with the given encoding.static String
encodePath(String path, String encoding)
Encode the given URI path with the given encoding.static String
encodePath(String path, Charset charset)
Encode the given URI path with the given encoding.static String
encodePathSegment(String segment, String encoding)
Encode the given URI path segment with the given encoding.static String
encodePathSegment(String segment, Charset charset)
Encode the given URI path segment with the given encoding.static String
encodePort(String port, String encoding)
Encode the given URI port with the given encoding.static String
encodePort(String port, Charset charset)
Encode the given URI port with the given encoding.static String
encodeQuery(String query, String encoding)
Encode the given URI query with the given encoding.static String
encodeQuery(String query, Charset charset)
Encode the given URI query with the given encoding.static String
encodeQueryParam(String queryParam, String encoding)
Encode the given URI query parameter with the given encoding.static String
encodeQueryParam(String queryParam, Charset charset)
Encode the given URI query parameter with the given encoding.static MultiValueMap<String,String>
encodeQueryParams(MultiValueMap<String,String> params)
Encode the query parameters from the givenMultiValueMap
with UTF-8.static String
encodeScheme(String scheme, String encoding)
Encode the given URI scheme with the given encoding.static String
encodeScheme(String scheme, Charset charset)
Encode the given URI scheme with the given encoding.static Object[]
encodeUriVariables(Object... uriVariables)
Convenience method to applyencode(String, Charset)
to all given URI variable values.static Map<String,String>
encodeUriVariables(Map<String,?> uriVariables)
Convenience method to applyencode(String, Charset)
to all given URI variable values.static String
encodeUserInfo(String userInfo, String encoding)
Encode the given URI user info with the given encoding.static String
encodeUserInfo(String userInfo, Charset charset)
Encode the given URI user info with the given encoding.static String
extractFileExtension(String path)
Extract the file extension from the given URI path.
构造器详细资料
UriUtils
public UriUtils()
方法详细资料
encodeScheme
public static String encodeScheme(String scheme, String encoding)
Encode the given URI scheme with the given encoding.- 参数:
scheme
- the scheme to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded scheme
encodeScheme
public static String encodeScheme(String scheme, Charset charset)
Encode the given URI scheme with the given encoding.- 参数:
scheme
- the scheme to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded scheme
- 从以下版本开始:
- 5.0
encodeAuthority
public static String encodeAuthority(String authority, String encoding)
Encode the given URI authority with the given encoding.- 参数:
authority
- the authority to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded authority
encodeAuthority
public static String encodeAuthority(String authority, Charset charset)
Encode the given URI authority with the given encoding.- 参数:
authority
- the authority to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded authority
- 从以下版本开始:
- 5.0
encodeUserInfo
public static String encodeUserInfo(String userInfo, String encoding)
Encode the given URI user info with the given encoding.- 参数:
userInfo
- the user info to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded user info
encodeUserInfo
public static String encodeUserInfo(String userInfo, Charset charset)
Encode the given URI user info with the given encoding.- 参数:
userInfo
- the user info to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded user info
- 从以下版本开始:
- 5.0
encodeHost
public static String encodeHost(String host, String encoding)
Encode the given URI host with the given encoding.- 参数:
host
- the host to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded host
encodeHost
public static String encodeHost(String host, Charset charset)
Encode the given URI host with the given encoding.- 参数:
host
- the host to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded host
- 从以下版本开始:
- 5.0
encodePort
public static String encodePort(String port, String encoding)
Encode the given URI port with the given encoding.- 参数:
port
- the port to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded port
encodePort
public static String encodePort(String port, Charset charset)
Encode the given URI port with the given encoding.- 参数:
port
- the port to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded port
- 从以下版本开始:
- 5.0
encodePath
public static String encodePath(String path, String encoding)
Encode the given URI path with the given encoding.- 参数:
path
- the path to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded path
encodePath
public static String encodePath(String path, Charset charset)
Encode the given URI path with the given encoding.- 参数:
path
- the path to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded path
- 从以下版本开始:
- 5.0
encodePathSegment
public static String encodePathSegment(String segment, String encoding)
Encode the given URI path segment with the given encoding.- 参数:
segment
- the segment to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded segment
encodePathSegment
public static String encodePathSegment(String segment, Charset charset)
Encode the given URI path segment with the given encoding.- 参数:
segment
- the segment to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded segment
- 从以下版本开始:
- 5.0
encodeQuery
public static String encodeQuery(String query, String encoding)
Encode the given URI query with the given encoding.- 参数:
query
- the query to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded query
encodeQuery
public static String encodeQuery(String query, Charset charset)
Encode the given URI query with the given encoding.- 参数:
query
- the query to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded query
- 从以下版本开始:
- 5.0
encodeQueryParam
public static String encodeQueryParam(String queryParam, String encoding)
Encode the given URI query parameter with the given encoding.- 参数:
queryParam
- the query parameter to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded query parameter
encodeQueryParam
public static String encodeQueryParam(String queryParam, Charset charset)
Encode the given URI query parameter with the given encoding.- 参数:
queryParam
- the query parameter to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded query parameter
- 从以下版本开始:
- 5.0
encodeQueryParams
public static MultiValueMap<String,String> encodeQueryParams(MultiValueMap<String,String> params)
Encode the query parameters from the givenMultiValueMap
with UTF-8.This can be used with
UriComponentsBuilder.queryParams(MultiValueMap)
when building a URI from an already encoded template.MultiValueMap<String, String> params = new LinkedMultiValueMap<>(2); // add to params... ServletUriComponentsBuilder.fromCurrentRequest() .queryParams(UriUtils.encodeQueryParams(params)) .build(true) .toUriString();
- 参数:
params
- the parameters to encode- 返回:
- a new
MultiValueMap
with the encoded names and values - 从以下版本开始:
- 5.2.3
encodeFragment
public static String encodeFragment(String fragment, String encoding)
Encode the given URI fragment with the given encoding.- 参数:
fragment
- the fragment to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded fragment
encodeFragment
public static String encodeFragment(String fragment, Charset charset)
Encode the given URI fragment with the given encoding.- 参数:
fragment
- the fragment to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded fragment
- 从以下版本开始:
- 5.0
encode
public static String encode(String source, String encoding)
Variant ofencode(String, Charset)
with a String charset.- 参数:
source
- the String to be encodedencoding
- the character encoding to encode to- 返回:
- the encoded String
encode
public 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. This is useful to ensure that the given String will be preserved as-is and will not have any o impact on the structure or meaning of the URI.- 参数:
source
- the String to be encodedcharset
- the character encoding to encode to- 返回:
- the encoded String
- 从以下版本开始:
- 5.0
encodeUriVariables
public static Map<String,String> encodeUriVariables(Map<String,?> uriVariables)
Convenience method to applyencode(String, Charset)
to all given URI variable values.- 参数:
uriVariables
- the URI variable values to be encoded- 返回:
- the encoded String
- 从以下版本开始:
- 5.0
encodeUriVariables
public static Object[] encodeUriVariables(Object... uriVariables)
Convenience method to applyencode(String, Charset)
to all given URI variable values.- 参数:
uriVariables
- the URI variable values to be encoded- 返回:
- the encoded String
- 从以下版本开始:
- 5.0
decode
public static String decode(String source, String encoding)
Decode the given encoded URI component.See
StringUtils.uriDecode(String, Charset)
for the decoding rules.- 参数:
source
- the encoded Stringencoding
- the character encoding to use- 返回:
- the decoded value
- 抛出:
IllegalArgumentException
- when the given source contains invalid encoded sequences- 另请参阅:
StringUtils.uriDecode(String, Charset)
,URLDecoder.decode(String, String)
decode
public static String decode(String source, Charset charset)
Decode the given encoded URI component.See
StringUtils.uriDecode(String, Charset)
for the decoding rules.- 参数:
source
- the encoded Stringcharset
- the character encoding to use- 返回:
- the decoded value
- 抛出:
IllegalArgumentException
- when the given source contains invalid encoded sequences- 从以下版本开始:
- 5.0
- 另请参阅:
StringUtils.uriDecode(String, Charset)
,URLDecoder.decode(String, String)
extractFileExtension
@Nullable public static String extractFileExtension(String path)
Extract the file extension from the given URI path.- 参数:
path
- the URI path (e.g. "/products/index.html")- 返回:
- the extracted file extension (e.g. "html")
- 从以下版本开始:
- 4.3.2