encode 与 new String(s.getBytes("UTF-8"),"ISO-8859-1");的不同
package Encoding;
?
import java.io.UnsupportedEncodingException;
?
public class Encoding {
public static void main(String[] args) {
// 同一字符串
String s = "http://localhost:8080/testURI/pages/result.jsp?result=%E6%88%90%E5%8A%9F";
try {
System.out.println( java.net.URLEncoder.encode(s, "utf-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
// ?utf-8编码
System.out.println(s.getBytes("UTF-8"));
String tryEncoding = new String(s.getBytes("UTF-8"),"ISO-8859-1");
System.out.println(tryEncoding);
System.out.println(new String(tryEncoding.getBytes("ISO-8859-1"),"UTF-8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
结果:
http%3A%2F%2Flocalhost%3A8080%2FtestURI%2Fpages%2Fresult.jsp%3Fresult%3D%25E6%2588%2590%25E5%258A%259F
[B@1fb8ee3
http://localhost:8080/testURI/pages/result.jsp?result=%E6%88%90%E5%8A%9F
http://localhost:8080/testURI/pages/result.jsp?result=%E6%88%90%E5%8A%9F
同一字符串 ,不同结果 可知与encodeURI 与encodeURIComponent类似