Java URLDecoder和URLEncoder对中文进行编码和解码
-
URLDecoder类包含一个decode(String s,String enc)静态方法,它可以将application/x-www-form-urlencoded MIME字符串转成普通字符串;
-
URLEncoder类包含一个encode(String s,String enc)静态方法,它可以将普通字符串转换成application/x-www-form-urlencoded MIME字符串。
package com.test; import com.sun.deploy.net.URLEncoder; import java.net.URLDecoder; public class JunitTestURLcode { @Test public void testURLcode()throws Exception{ //将application/x-www-form-urlencoded字符串转换成普通字符串 //采用UTF-8字符集进行解码 System.out.println(URLDecoder.decode("%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6", "UTF-8")); //采用GBK字符集进行解码 System.out.println(URLDecoder.decode("%B1%B1%BE%A9%B4%F3%D1%A7", "GBK")); // 将普通字符串转换成application/x-www-form-urlencoded字符串 //采用utf-8字符集 System.out.println(URLEncoder.encode("北京大学", "UTF-8")); //采用GBK字符集 System.out.println(URLEncoder.encode("北京大学", "GBK")); } }