1 import java.io.UnsupportedEncodingException; 2 /** 3 * url转码、解码 4 */ 5 public class UrlUtil { 6 private final static String ENCODE = "GBK"; 7 /** 8 * URL 解码 9 * 10 */ 11 public static String getURLDecoderString(String str) { 12 String result = ""; 13 if (null == str) { 14 return ""; 15 } 16 try { 17 result = java.net.URLDecoder.decode(str, ENCODE); 18 } catch (UnsupportedEncodingException e) { 19 e.printStackTrace(); 20 } 21 return result; 22 } 23 /** 24 * URL 转码 25 */ 26 public static String getURLEncoderString(String str) { 27 String result = ""; 28 if (null == str) { 29 return ""; 30 } 31 try { 32 result = java.net.URLEncoder.encode(str, ENCODE); 33 } catch (UnsupportedEncodingException e) { 34 e.printStackTrace(); 35 } 36 return result; 37 } 38 39 /** 40 * 41 */ 42 public static void main(String[] args) { 43 String str = "测试1"; 44 System.out.println(getURLEncoderString(str)); 45 System.out.println(getURLDecoderString(str)); 46 47 } 48 49 }