web字符转义
一、URLDecoder、URLEncoder
URLDecoder和URLEncoder它的作用主要是用于普通字符串和application/x-www-form-rulencoded MIME字符串之间的转换。
当URL地址里包含非西欧字符的字符串时(比如中文),系统会将这些非西欧转换成如图所示的特殊字符串,那么编码过程中可能涉及将普通字符串和这种特殊字符串的相关转换,这就是需要使用URLDecoder和URLEncoder类。
public static void main(String[] args) throws UnsupportedEncodingException {String urlStr = "http://172.168.1.1192.168.1.1:8888/store/get?id=dc2-80d";String urlStrEncode = URLEncoder.encode(urlStr, "utf-8" );String urlStrDecode = URLDecoder.decode(urlStrEncode, "utf-8");System.out.println("urlStrEncode = " + urlStrEncode);System.out.println("urlStrDecode = " + urlStrDecode);// 将普通字符串转换成 application/x-www-form-urlencoded字符串String str = URLEncoder.encode("默默前行", "UTF-8");System.out.println("str = " + str);// 将application/x-www-form-urlencoded字符串转换成普通字符串String keyWord = URLDecoder.decode("%E9%BB%98%E9%BB%98%E5%89%8D%E8%A1%8C", "UTF-8");System.out.println("keyWord = " + keyWord);
}
当URL地址中仅包含普通非中文字符串和application/x-www-form-urlencoded MIME字符串无须转换,而包含中文字符串的普通字符串则需要转换,换句话说,也就是说URL地址中有"中文字符串"传递时,才会考虑用到上面提到的两个类,这样就可以将传递过来的中文接受后,再还原成原来的中文字符串.如不转换,则通过URL传递过来的中文字符中会变成乱码,无法还原了。
二、符号的转义
< 、>、 <=、 >=、 "、 '、
< | < | < | 小于号 |
---|---|---|---|
> | > | > | 大于号 |
≤ | ≤ | ≤ | 小于等于号 |
≥ | ≥ | ≥ | 大于等于号 |
" | " | " | 引号 |
“ | “ | � | 左双引号 |
” | ” | � | 右双引号 |
‘ | ‘ | � | 左单引号 |
’ | ’ | � | 右单引号 |
+ | URL 中+号表示空格 | %2B |
---|---|---|
空格 | URL中的空格可以用+号或者编码 | %20 |
/ | 分隔目录和子目录 | %2F |
? | 分隔实际的URL和参数 | %3F |
% | 指定特殊字符 | %25 |
# | 表示书签 | %23 |
& | URL 中指定的参数间的分隔符 | %26 |
= | URL 中指定参数的值 | %3D |