目 录CONTENT

文章目录

Java实用工具类五:URL转码、解码类

芈亓
2022-03-11 / 0 评论 / 0 点赞 / 54 阅读 / 3,430 字 / 正在检测是否收录...
温馨提示:
本文最后更新于 2022-04-15,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
  • 此文仅对自己工作中用到的类进行总结,方便以后的使用。
package com.cn.hnust.util;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
/**
 * url转码、解码
 *
 * @author
 * @date 
 */
public class UrlUtil {
    private final static String ENCODE = "GBK"; 
    /**
     * URL 解码
     *
     * @return String
     * @author lifq
     * @date 2015-3-17 下午04:09:51
     */
    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
     * @author lifq
     * @date 2015-3-17 下午04:10:28
     */
    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;
     }
    /**  
     * 正则替换  
     * @param url  
     * @param name  
     * @param value  
     * @return  
     */  
    public static String replaceUrlParameterReg(String url, String name, String value) {  
        if (StringUtils.isNotBlank(url) && StringUtils.isNotBlank(value)) {  
            url = url.replaceAll("(" + name + "=[^&]*)", name + "=" + value);  
        }  
        return url;  
    }  
    /**  
     * 正则替换  
     * @param url  
     * @param name  
     * @param value  
     * @return  
     */  
    public static String getUrlParameterReg(String url, String name) {

        Map<String, String> mapRequest = new HashMap<String, String>();

          String[] arrSplit=null;

        String strUrlParam=TruncateUrlPage(url);
        if(strUrlParam==null)
        {
            return "";
        }
          //每个键值为一组 www.2cto.com
        arrSplit=strUrlParam.split("[&]");
        for(String strSplit:arrSplit)
        {
              String[] arrSplitEqual=null;         
              arrSplitEqual= strSplit.split("[=]");

              //解析出键值
              if(arrSplitEqual.length>1)
              {
                  //正确解析
                  mapRequest.put(arrSplitEqual[0], arrSplitEqual[1]);

              }
              else
              {
                  if(arrSplitEqual[0]!="")
                  {
                  //只有参数没有值,不加入
                  mapRequest.put(arrSplitEqual[0], "");       
                  }
              }
        }   
        return mapRequest.get(name);  
    }  
    /**
     * 去掉url中的路径,留下请求参数部分
     * @param strURL url地址
     * @return url请求参数部分
     */
    private static String TruncateUrlPage(String strURL)
    {
    String strAllParam=null;
      String[] arrSplit=null;

      strURL=strURL.trim().toLowerCase();

      arrSplit=strURL.split("[?]");
      if(strURL.length()>1)
      {
          if(arrSplit.length>1)
          {
                  if(arrSplit[1]!=null)
                  {
                  strAllParam=arrSplit[1];
                  }
          }
      }

    return strAllParam;   
    }
    public static void main(String[] args) {
        String str ="/activePage?wxto=0C29975119DE037B4F25CD119CF4CF11&activity2018topid=oz9Zj07Jg5O9IfAyVauT-GL-CSfM&activity2018topunid=o-NUA0l8fxm2_5fmK_ocpyxrP1dE&subscribe=0&timestamp=1516008942920&-NUA0l8fxm2_5fmK_ocpyxrP1dE&oaccesstoken=5_xJYY3gnUTD4pqJFc7_ltbvclb0ZRKOFBEc5BXAwi7vjZndRAN58AkPazqUJDGzBbrw-J1ag1PF0C-S39SqJflA&m=1&wxt=C9A58DA3804E46981BF4B78C8C862743&-NUA0oHjJjCcyGe-wJdEFYjwrfU&-NUA0oHjJjCcyGe-wJdEFYjwrfU&-NUA0oHjJjCcyGe-wJdEFYjwrfU";
        String m = "9j70Dj%2BpVA64qK9%2FeOjsg2dP3biHh6imIC6WFXtIRcR6nHwDkjHPNOL9zUi7fmpcbgJP3u%2Bh3VUTfDO0B2fsbKWsizlqOKdXJovkg3HZMTxAIcByvYvtHoCyd9iOgUxoDqslzoMunvnEzPS8FbYc%2Fjkd4y47zhKk%2FUTE4GGm8ZM5Ns5eYwZSFL7A8pX0U%2BXfsMR%2FGf5z2pci6a5BJaki4e0z0Pmh5sPsjytmKdjdssHQ98iZ8ge3mXJm7G%2FaSJ3e%2FHvNENLv0JRJf7q1bFu180RZEtsMRs%2BWdesTuJamtJBYtLxQEYaCMygRcmd%2F1VdSqfLnuZqcZAzT6GAmvBwZyfzCK9LffGsyZGWce%2B0g2R%2FTCKTTtNpWWCJb0e7Gs0g82DmBNV4jh3hrOAmeLNwpu0M%2B049%2FHrDxAZPvXowCb5OLb7if%2BVqBpYkphGssurkqLV2KTXx3vhjl9vIiMQnD4397oNYf81P%2BBWFPBFhPQSfGaxZGZbAlPgkluR4%2Fj75f9SeZlcWFMnl%2BYKYtopw1gw%3D%3D";
        System.out.println(getURLEncoderString(m));
        System.out.println(getURLDecoderString(m));
      //System.out.println( getUrlParameterReg(str,"wxto"));
    }
}
0

评论区