java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " 0"


value = URLDecoder.decode(request.getParameter(paraName), "UTF-8");

前端用了 encodeURI 來編碼參數,后端用 URLDecoder 解碼,報錯:

java.lang.IllegalArgumentException: URLDecoder: Illegal hex characters in escape (%) pattern - For input string: " 0"

 

http://stackoverflow.com/questions/6067673/urldecoder-illegal-hex-characters-in-escape-pattern-for-input-string

Characters that get encoded have % and + signs in them, so although this helps with % and + characters in a string, it also doesn't decode things like %20 (space) because you are taking out the percent before decoding.

A solution is to replace %2B (+) and %25 (%) instead. Something like:

 public static String replacer(StringBuffer outBuffer) { String data = outBuffer.toString(); try { data = data.replaceAll("%(?![0-9a-fA-F]{2})", "%25"); data = data.replaceAll("\\+", "%2B"); data = URLDecoder.decode(data, "utf-8"); } catch (Exception e) { e.printStackTrace(); } return data; }

"+" is a special character which denotes a quantifier meaning one of more occurrences. So one should use "\+"

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM