URLDecoder.decode(String str,String charSet)的大致实现原理
2016-01-08 16:30
92 查看
URL编码 百分号编码 URLDecoder.decode的大致实现原理
Java代码
package com.dt.test;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/***
* URL编码又叫百分号编码 URLDecoder.decode的大致实现原理
*/
class testURLEncode {
public void testURLEncode() {
String testString;
try {
testString = URLEncoder.encode("中文", "utf-8");
System.out.println("testString : " + testString);
testString = testString.replace("%", "");
int length = testString.length() / 2;
byte[] data = new byte[length];
for (int i = 0; i < length; i++) {
data[i] = (byte) Integer.parseInt(testString.substring(2 * i,
2 * i + 2), 16);
}
String result = new String(data, "utf-8");
System.out.println("result : " + result);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void testURLEncodeGBK() {
String testString;
String testString0;
try {
testString = URLEncoder.encode("中文", "utf-8");
testString0 = testString;
System.out.println("testString : " + testString);
testString = URLDecoder.decode(testString0,"GBK");
System.out.println("decode : " + testString);
testString = URLDecoder.decode(testString0,"utf-8");
System.out.println("decode : " + testString);
testString = URLEncoder.encode("中文", "GBK");
System.out.println("decode : " + testString);
testString = URLDecoder.decode(testString,"GBK");
System.out.println("decode : " + testString);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
new testURLEncode().testURLEncode();
new testURLEncode().testURLEncodeGBK();
}
}
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>
Java代码
package com.dt.test;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
/***
* URL编码又叫百分号编码 URLDecoder.decode的大致实现原理
*/
class testURLEncode {
public void testURLEncode() {
String testString;
try {
testString = URLEncoder.encode("中文", "utf-8");
System.out.println("testString : " + testString);
testString = testString.replace("%", "");
int length = testString.length() / 2;
byte[] data = new byte[length];
for (int i = 0; i < length; i++) {
data[i] = (byte) Integer.parseInt(testString.substring(2 * i,
2 * i + 2), 16);
}
String result = new String(data, "utf-8");
System.out.println("result : " + result);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public void testURLEncodeGBK() {
String testString;
String testString0;
try {
testString = URLEncoder.encode("中文", "utf-8");
testString0 = testString;
System.out.println("testString : " + testString);
testString = URLDecoder.decode(testString0,"GBK");
System.out.println("decode : " + testString);
testString = URLDecoder.decode(testString0,"utf-8");
System.out.println("decode : " + testString);
testString = URLEncoder.encode("中文", "GBK");
System.out.println("decode : " + testString);
testString = URLDecoder.decode(testString,"GBK");
System.out.println("decode : " + testString);
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
}
public static void main(String[] args) {
new testURLEncode().testURLEncode();
new testURLEncode().testURLEncodeGBK();
}
}
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>
相关文章推荐
- UITextView高度根据内容变化
- 网页代码测试工具集合
- 大端小端
- 2015年就这样过去了
- loadrunner---压力机 出现TIME_WAIT状态的解决方法
- ExtJs中使用highChart
- Mysql 查看连接数 状态等参数
- 十个JAVA程序员容易犯的错误
- 上传图片预览
- 计算Windows下目录大小
- 新视觉影院yy6080.org视频的抓取
- mark down
- 苹果分辨率与大小
- VS2010解决办法☞LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- 处理器指令集 i386 x86_64 arm64 armv7 armv7s
- GDB 的命令详解
- PS模糊滤镜将汽车照片打造移轴电影画面效果
- javascript弹出层
- Ubuntu、CentOS、debian下Zabbix agent自动安装脚本
- java泛型