当前位置: 代码迷 >> Java相关 >> 请问:J2SE URLDecode出错
  详细解决方案

请问:J2SE URLDecode出错

热度:3848   发布时间:2013-02-25 21:49:59.0
请教:J2SE URLDecode出错
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

public class obscureStr {
public static void main(String[] args) {
String a = "你好 % 18.0000";
String b;
try {
b = URLDecoder.decode(a, "UTF-8");
System.out.println(b);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}

出错:

Exception in thread "main" java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " 1"
at java.net.URLDecoder.decode(Unknown Source)
at obscureStr.main(obscureStr.java:9)


请问为什么呢?

------解决方案--------------------------------------------------------
Java code
String a = "你好 % 18.0000";        String b;        try        {            b = URLEncoder.encode(a, "UTF-8");            System.out.println(b);            a = URLDecoder.decode(b, "UTF-8");            System.out.println(a);        }        catch (UnsupportedEncodingException e)        {            e.printStackTrace();        }
------解决方案--------------------------------------------------------
探讨

zhouyuqwert,你好,能说一下原因吗?

------解决方案--------------------------------------------------------
额 URLEncoder主要为了解决URL中存在特殊字符,比如& 空格 中文字符之类的 避免URL无法被正确解析