使用URLEncoder、URLDecoder进行URL参数的转码与解码

来源:互联网 发布:dbc2000 装备数据库 编辑:程序博客网 时间:2024/06/13 01:21

url后参数的转码与解码

 

import java.net.URLDecoder;
import java.net.URLEncoder;

 

 

  String strTest = "?=abc?中%1&2<3,4>";
  strTest = URLEncoder.encode(strTest, "UTF-8");
  System.out.println(strTest);
  strTest = URLDecoder.decode(strTest,"UTF-8");
  System.out.println(strTest);

 

 

执行结果:

%3F%3Dabc%3F%E4%B8%AD%251%262%3C3%2C4%3E
?=abc?中%1&2<3,4>

 

 

 

jdk相关说明:

String java.net.URLEncoder.encode(String s, String enc) throwsUnsupportedEncodingException

Translates a string into application/x-www-form-urlencoded format using a specific encoding scheme. This method uses the supplied encoding scheme to obtain the bytes for unsafe characters.

Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilites.

Parameters:
s String to be translated.
enc The name of a supported character encoding.
Returns:
the translated String.
Throws:
UnsupportedEncodingException - If the named encoding is not supported
Since:
1.4
See Also:
URLDecoder.decode(java.lang.String, java.lang.String)

 

String java.net.URLDecoder.decode(String s, String enc) throwsUnsupportedEncodingException

Decodes a application/x-www-form-urlencoded string using a specific encoding scheme. The supplied encoding is used to determine what characters are represented by any consecutive sequences of the form "%xy".

Note: The World Wide Web Consortium Recommendation states that UTF-8 should be used. Not doing so may introduce incompatibilites.

Parameters:
s the String to decode
enc The name of a supported character encoding.
Returns:
the newly decoded String
Throws:
UnsupportedEncodingException - If character encoding needs to be consulted, but named character encoding is not supported
Since:
1.4
See Also:
URLEncoder.encode(java.lang.String, java.lang.String)
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 龙吐珠花冻着了怎么办 发财树浇水多了怎么办 发财树根部烂了怎么办 发财树的根烂了怎么办 淘米水发酵臭了怎么办 眼睛下的小细纹怎么办 内眼角开的太小怎么办 做完美瞳眼睛红怎么办 纹完眼线眼睛红怎么办 21岁眼下小细纹怎么办 20岁眼部有细纹怎么办 纹身几天后晕色怎么办 屁眼长了痔疮大怎么办 苹果os系统坏了怎么办 苹果6出现白苹果怎么办 苹果平板白屏了怎么办 腿上的皮肤很干怎么办 鱼身上掉了鳞片怎么办 患上恋爱恐慌症怎么办 我觉得活着好累怎么办 我的世界遇到him怎么办 孕妇闻了樟脑球怎么办 电脑不能识别u盘怎么办 电脑识别不了u盘怎么办 神之子武器爆了怎么办 中控本考试没过怎么办 震后安全逃生后怎么办 逃出电梯游戏2关怎么办 车尾灯灯罩破了怎么办 着火了吸入了烟怎么办 多媒课件着火了怎么办 用手楚了眼睛疼怎么办 眼睛又痒又干涩怎么办 比熊吃了奥利奥怎么办 怪物猎人x迷路了怎么办 3ds被破解任天堂怎么办 多肉植物焉了怎么办 文竹的叶子干了怎么办 散白酒卖不出去怎么办 自酿葡萄酒太甜怎么办 自酿葡萄酒太酸怎么办