All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.wzc789376152.utils.UrlUtil Maven / Gradle / Ivy

package com.github.wzc789376152.utils;

import java.util.Map;
import java.util.Set;

/**
 * @author zhongquanliang 2020/9/18.
 */
public class UrlUtil {

    /**
     * Get url join
     *
     * @param url    base url
     * @param params params
     * @return String
     */
    public static String urlJoin(String url, Map params) {
        StringBuilder endUrl = new StringBuilder(url);
        if (null == params) {
            return url;
        }
        boolean isFirst = true;
        Set> entrySet = params.entrySet();
        for (Map.Entry entry : entrySet) {
            if (isFirst && !url.contains("?")) {
                isFirst = false;
                endUrl.append("?");
            } else {
                endUrl.append("&");
            }
            endUrl.append(entry.getKey());
            endUrl.append("=");
            endUrl.append(entry.getValue());
        }
        return endUrl.toString();
    }

    /**
     * Replace '//' with '/' in the url.
     *
     * @param url url
     * @return processed url
     */
    public static String simplifyUrl(String url) {
        int index = url.indexOf("://");
        if (index != -1) {
            String urlHead = url.substring(0, index + 2);
            String urlTail = url.substring(index + 2).replaceAll("/+", "/");
            return urlHead + urlTail;
        } else {
            return url.replaceAll("/+", "/");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy