http get请求参数封装成map

http get请求参数工具类:

public class UrlSpiltUtils {
	/**
	 * 小程序拼接
	 * @param url
	 * @return
	 */
	public static String urlSpilt(String url) {
		if (StringUtils.isNotBlank(url)) {
			if (url.startsWith("https://shop.m.suning.com/")) {//店铺页拼接url
				String shopHtml = url.split("/")[3].split("\\.")[0];
				String relShopHtml;
				if (8 == shopHtml.length()) {
					relShopHtml = "00" + shopHtml;
				} else if (10 == shopHtml.length()) {
					relShopHtml = shopHtml;
				} else {
					return null;
				}
				return "/pages/shop/index/index?shopId=" + relShopHtml;
			} else if (url.contains("/pages/cuxiao/")) {//活动促销页-原生
				return url;
			} else if (url.contains("cuxiao.m.suning.com")) {//活动促销页-H5页面(泰坦)
				return "/pages/webView/index?webViewSrc=" + url;
			} else if (url.startsWith("https://m.suning.com/product/")) {//四级页
				String shopId = url.split("/")[4];
				String productId = url.split("/")[5].split("\\.")[0];
				return "/pages/fourth/fourth?productId=" + productId + "&shop=" + shopId + "&title=";
			}
		}
		return null;
	}

	/**
	 * 将url中参数封装成map
	 * @param url
	 * @return
	 */
	public static Map<String, String> getUrlMap(String url) {
		Map<String, String> map = new HashMap<String, String>();
		URL urls = null;
		try {
			urls = new URL(url);
			String param = urls.getQuery();
			if (StringUtils.isNotBlank(param)) {
				String[] arr = param.split("&");
				for (String s : arr) {
					String key = s.split("=")[0];
					String value = s.split("=")[1];
					map.put(key, value);
				}
			}
		} catch (MalformedURLException e) {
			log.warn("getUrlMap", "MalformedURLException e={}" + e);
		}
		return map;
	}

	/**
	 * hashMap 转化成表单字符串
	 * @param map
	 * @return
	 */
	public static String map2Form(Map<String, String> map) {
		StringBuilder stringBuilder = new StringBuilder();
		if (map == null || map.size() == 0) {
			return stringBuilder.toString();
		} else {
			for (Map.Entry<String, String> entry : map.entrySet()) {
				stringBuilder.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
			}
			return stringBuilder.substring(0, stringBuilder.length() - 1);
		}
	}


  /**
     * 表单字符串转化成 hashMap
     *
     * @param orderinfo
     * @return
     */
    public static HashMap<String, String> form2Map( String orderinfo) {
        String listinfo[];
        HashMap<String, String> map = new HashMap<String, String>();
        listinfo = orderinfo.split("&");
        for(String s : listinfo)
        {
            String list[]  = s.split("=");
            if(list.length>1)
            {
                map.put(list[0], list[1]);
            }
        }
        return map;
    }


}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值