URL符号化パーセンテージ符号化URLDecoder.decodeの大まかな実現原理
2428 ワード
URL符号化パーセンテージ符号化URLDecoder.decodeの大まかな実現原理
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) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public static void main(String[] args) {
new testURLEncode().testURLEncode();
new testURLEncode().testURLEncodeGBK();
}
}
testString : %E4%B8%AD%E6%96%87
result :
testString : %E4%B8%AD%E6%96%87
decode :
decode :
decode : %D6%D0%CE%C4
decode :