url 转码、解码

作者: keyinuo | 来源:发表于2019-02-27 14:29 被阅读28次
    
    public class UrlUtil {
    
        private final static String ENCODE = "UTF-8";
    
        /**
         * URL 解码
         * @return String
         */
        public static String getURLDecoderString(String str) {
            String result = "";
            if (null == str) {
                return "";
            }
            try {
                result = java.net.URLDecoder.decode(str, ENCODE);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * URL 转码
         * @return String
         */
        public static String getURLEncoderString(String str) {
            String result = "";
            if (null == str) {
                return "";
            }
            try {
                result = java.net.URLEncoder.encode(str, ENCODE);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return result;
        }
    
        /**
         * @return void
         */
        public static void main(String[] args) {
            String str = "测试1";
            System.out.println(getURLEncoderString(str));
            System.out.println(getURLDecoderString(str));
        }
    
    }
    
    

    相关文章

      网友评论

        本文标题:url 转码、解码

        本文链接:https://www.haomeiwen.com/subject/keqfuqtx.html