1.URLEncode - URLDecode 编码加密 这两个类是jdk中封装的类,不需要额外导入jar包
String para = java.net.URLEncoder.encode("乱码", "utf-8"); - 编码
String para1 = java.net.URLDecoder.decode(para, "utf-8"); - 解码
para: %E4%B9%B1%E7%A0%81
para1: 乱码
String test = new String(request.getParameter("zhongguo").getBytes("iso8859_1"));
zhongguo = java.net.URLDecoder.decode(test,"utf-8");
2.编码中的特殊字符: http://blog.sina.com.cn/s/blog_6b2d01f60101763v.html
URLencode.encode 是'.', '-', '*', '_'符号不编码,而URL传输需要字符串是 "-", ".", "_", "~"不编码。那么可以这样的
String URL="www.baidu.com";
String ENCODING="utf-8";
String strURL= URLEncoder.encode(URL, ENCODING).replace("*","*").replace("~", "~").replace("+"," ");
3.使用URLDecoder.decode() 方法接收参数时如果参数中含有“ %”这个字符,就会抛异常 java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern -,
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
/**
* Base64解密
* @param key
* @return
* @throws Exception
*/
public static byte[] decoderBase64(String key) throws Exception {
return (new BASE64Decoder()).decodeBuffer(key);
}
* Base64加密
* @param key
* @return
* @throws Exception
*/
public static String encoderBase64(byte[] key) throws Exception {
return (new BASE64Encoder()).encodeBuffer(key);
}