-
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")); } }