public static String getURLEncoderString(String str) { String result = ""; if (null == str) { return ""; } try { result = java.net.URLEncoder.encode(str, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } public static String URLDecoderString(String str) { String result = ""; if (null == str) { return ""; } try { result = java.net.URLDecoder.decode(str, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }
注意:java.net的url編碼方法在對一些特殊符號編碼時有個bug,如:+號decord時,+號會變成空格,需要把+號替換成%2B,str=str.replaceAll("\\+", "%2B");
