【URL 的编码、解码】

工具类

  1 /**
  2  * URLEncodeTest.java
  3  * weixinTest
  4  *
  5  * Function: TODO 
  6  *
  7  *   ver     date              author
  8  * ──────────────────────────────────
  9  *            2018年3月23日     GuoYihua
 10  *
 11  * Copyright (c) 2018, TNT All Rights Reserved.
 12 */
 13 
 14 package weixinTest;
 15 
 16 import java.io.UnsupportedEncodingException;
 17 import java.net.URLDecoder;
 18 import java.net.URLEncoder;
 19 
 20 /**
 21  * ClassName:URLEncodeTest(URL编码解码)
 22  * @Function TODO ADD FUNCTION
 23  * @Reason     TODO ADD REASON
 24  *
 25  * @author  GuoYihua
 26  * @version  
 27  * @since    Ver 1.1
 28  * @Date     2018年3月23日        上午9:21:42
 29  *
 30  * @see      
 31  *  
 32  */
 33 public class URLEncodeUtil {
 34 
 35     public static void main(String[] args) {
 36         String URL ="https://www.cnblogs.com";
 37         System.out.println(urlEncodeUTF8(URL));
 38         System.out.println(urlEncodeGb2312(URL));
 39         System.out.println(urlDecodeUTF8("https%3A%2F%2Fwww.cnblogs.com"));
 40         System.out.println(urlDecodeGb2312("https%3A%2F%2Fwww.cnblogs.com"));
 41     }
 42     
 43    /**
 44     * 
 45     * urlEncodeUTF8:(详述: URL 编码)
 46     * @Function TODO ADD FUNCTION
 47     * @Reason     TODO ADD REASON
 48     * @param  @param URL
 49     * @param  @return    设定文件
 50     * @return String    DOM对象
 51     * @throws 
 52     * @version  
 53     * @author   GuoYihua
 54     * @Date    2018年3月23日        上午9:22:36
 55     * @since  Ver 1.1
 56     */
 57     public static String urlEncodeUTF8(String URL) {
 58         String result = URL;
 59         try {
 60             result = URLEncoder.encode(URL, "UTF-8");
 61         } catch (UnsupportedEncodingException e) {
 62             e.printStackTrace();
 63         }
 64         return result;
 65     }
 66 
 67 
 68     /**
 69      * 
 70      * urlDecodeUTF8:( 详述:URL 解码)
 71      * @Function TODO ADD FUNCTION
 72      * @Reason     TODO ADD REASON
 73      * @param  @param URL
 74      * @param  @return    设定文件
 75      * @return String    DOM对象
 76      * @throws 
 77      * @version  
 78      * @author   GuoYihua
 79      * @Date    2018年3月23日        上午9:23:39
 80      * @since  Ver 1.1
 81      */
 82     public static String urlDecodeUTF8(String URL) {
 83         String result = "";
 84         try {
 85             result = URLDecoder.decode(URL, "UTF-8");
 86         } catch (UnsupportedEncodingException e) {
 87             e.printStackTrace();
 88         }
 89         return result;
 90     }
 91     
 92     /**
 93      * 
 94      * urlDecodeGb2312:(详述:URL 解码)
 95      * @Function TODO ADD FUNCTION
 96      * @Reason     TODO ADD REASON
 97      * @param  @param URL
 98      * @param  @return    设定文件
 99      * @return String    DOM对象
100      * @throws 
101      * @version  
102      * @author   GuoYihua
103      * @Date    2018年3月23日        上午9:24:01
104      * @since  Ver 1.1
105      */
106     public static String urlDecodeGb2312(String URL) {
107         String result = "";
108         try {
109             result = URLDecoder.decode(URL, "gb2312");
110         } catch (UnsupportedEncodingException e) {
111             e.printStackTrace();
112         }
113         return result;
114     }
115     
116     
117    /**
118     * 
119     * urlEncodeGb2312:(详述:URL 编码)
120     * @Function TODO ADD FUNCTION
121     * @Reason     TODO ADD REASON
122     * @param  @param URL
123     * @param  @return    设定文件
124     * @return String    DOM对象
125     * @throws 
126     * @version  
127     * @author   GuoYihua
128     * @Date    2018年3月23日        上午9:24:15
129     * @since  Ver 1.1
130     */
131     public static String urlEncodeGb2312(String URL) {
132         String result = URL;
133         try {
134             result = URLEncoder.encode(URL, "gb2312");
135         } catch (UnsupportedEncodingException e) {
136             e.printStackTrace();
137         }
138         return result;
139     }
140     
141     
142 }
原文地址:https://www.cnblogs.com/angelye/p/8628456.html