public class PercentCodec extends Object implements Serializable
百分号编码会对 URI 中不允许出现的字符或者其他特殊情况的允许的字符进行编码,对于被编码的字符,最终会转为以百分号"%“开头,后面跟着两位16进制数值的形式。 举个例子,空格符(SP)是不允许的字符,在 ASCII 码对应的二进制值是"00100000”,最终转为"%20"。
对于不同场景应遵循不同规范:
| Constructor and Description |
|---|
PercentCodec()
构造
[a-zA-Z0-9]默认不被编码 |
PercentCodec(BitSet safeCharacters)
构造
|
| Modifier and Type | Method and Description |
|---|---|
PercentCodec |
addSafe(char c)
增加安全字符
安全字符不被编码 |
String |
encode(CharSequence path,
Charset charset,
char... customSafeChar)
将URL中的字符串编码为%形式
|
static PercentCodec |
of(CharSequence chars)
创建PercentCodec,使用指定字符串中的字符作为安全字符
|
static PercentCodec |
of(PercentCodec codec)
从已知PercentCodec创建PercentCodec,会复制给定PercentCodec的安全字符
|
PercentCodec |
or(PercentCodec codec)
增加安全字符到挡墙的PercentCodec
|
PercentCodec |
orNew(PercentCodec codec)
组合当前PercentCodec和指定PercentCodec为一个新的PercentCodec,安全字符为并集
|
PercentCodec |
removeSafe(char c)
移除安全字符
安全字符不被编码 |
PercentCodec |
setEncodeSpaceAsPlus(boolean encodeSpaceAsPlus)
是否将空格编码为+
如果为 true,则将空格编码为"+",此项只在"application/x-www-form-urlencoded"中使用如果为 false,则空格编码为"%20",此项一般用于URL的Query部分(RFC3986规范) |
public PercentCodec()
public PercentCodec(BitSet safeCharacters)
safeCharacters - 安全字符,安全字符不被编码public static PercentCodec of(PercentCodec codec)
codec - PercentCodecpublic static PercentCodec of(CharSequence chars)
chars - 安全字符合集public PercentCodec addSafe(char c)
c - 字符public PercentCodec removeSafe(char c)
c - 字符public PercentCodec or(PercentCodec codec)
codec - PercentCodecpublic PercentCodec orNew(PercentCodec codec)
codec - PercentCodecpublic PercentCodec setEncodeSpaceAsPlus(boolean encodeSpaceAsPlus)
true,则将空格编码为"+",此项只在"application/x-www-form-urlencoded"中使用false,则空格编码为"%20",此项一般用于URL的Query部分(RFC3986规范)encodeSpaceAsPlus - 是否将空格编码为+public String encode(CharSequence path, Charset charset, char... customSafeChar)
path - 需要编码的字符串charset - 编码, null返回原字符串,表示不编码customSafeChar - 自定义安全字符Copyright © 2025. All rights reserved.