import java.io.UnsupportedEncodingException; import java.net.URLDecoder; import java.net.URLEncoder; public class a { public static void main(String[] args) throws UnsupportedEncodingException { //存在转义字符,进行转换后显示正常。 String str="http%3A%2F%2F183.196.225.226%3A6601%2Fbase-server%2Fa"; System.out.println(URLDecoder.decode(str,"UTF-8")); //不存在转义字符,进行转换后也没有问题。 String str_db="http://183.196.225.226:6601"; System.out.println(URLDecoder.decode(str_db,"UTF-8"));
//显示结果:
// http://183.196.225.226:6601/base-server/a
// http://183.196.225.226:6601
//======================================================================================= /* //来点复杂的 String strTest = "?=abc?中%1&2<3,4>"; strTest = URLEncoder.encode(strTest, "UTF-8"); System.out.println(strTest); strTest = URLDecoder.decode(strTest,"UTF-8"); System.out.println(strTest); System.out.println("采用UTF-8字符集进行解码:"); String keyWord = URLDecoder.decode("%E5%A4%A9%E6%B4%A5%E5%A4%A7%E5%AD%A6+Rico", "UTF-8"); System.out.println(keyWord); System.out.println(" 采用GBK字符集进行解码:"); System.out.println(URLDecoder.decode("%E5%A4%A9%E6%B4%A5%E5%A4%A7%E5%AD%A6+Rico", "GBK")); // 将普通字符串转换成application/x-www-form-urlencoded字符串 System.out.println(" 采用utf-8字符集:"); String urlStr = URLEncoder.encode("天津大学", "utf-8"); System.out.println(urlStr); System.out.println(" 采用GBK字符集:"); String urlStr2 = URLEncoder.encode("天津大学", "GBK"); System.out.println(urlStr2); */ } }