#1. URLDecoder 和 URLEncoder
控制台输出:public static void main(String[] args) { try { String str = "啦啦啦"; str = URLEncoder.encode(str, "UTF-8"); System.out.println(str); str = URLDecoder .decode(str, "UTF-8"); System.out.println(str); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
%E5%95%A6%E5%95%A6%E5%95%A6
啦啦啦
以上两种都是字符串和 application/x-www-form-urlencoded 格式之间的转换
#2. str.getByte()
JVM内部的String,Char都是用unicode存储(没有任何编码)public static void main(String[] args) { try { String str = "啦啦啦"; str = new String(str.getBytes("UTF-8"),"UTF-8"); System.out.println(str); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } }
String.getBytes("encoding")是把JVM内存中unicode形式的String按encoding制定的编码,转成字节流
String std=new String(byte b[],"encoding")是把encoding编码的字节流b,转换成String