com.suchtool.nicetool.util.web.http.url.HttpUrlUtil Maven / Gradle / Ivy
package com.suchtool.nicetool.util.web.http.url;
import com.suchtool.nicetool.util.web.http.url.annotation.UrlParamProperty;
import org.springframework.util.CollectionUtils;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Field;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
public class HttpUrlUtil {
/**
* 拼接URL(自动添加或者去除首尾的/)
*
* @param fragments URL片段
* @param addScheme 是否自动添加协议头
* @return 拼接好的URL
*/
public static String joinUrl(List fragments, boolean addScheme) {
StringBuilder result = new StringBuilder();
for (String fragment : fragments) {
if (fragment == null || fragment.isEmpty()) {
continue;
}
if (result.length() > 0 && result.charAt(result.length() - 1) != '/') {
result.append('/');
}
if (fragment.charAt(0) == '/') {
fragment = fragment.substring(1);
}
result.append(fragment);
}
String url = result.toString();
if (addScheme) {
if (!url.startsWith("http")
&& !url.startsWith("https")) {
url = "http://" + url;
}
}
return url;
}
/**
* 拼接URL(自动添加或者去除首尾的/。如果没有协议头,会自动添加)
*
* @param fragments URL片段
* @return 拼接好的URL
*/
public static String joinUrl(List fragments) {
return joinUrl(fragments, true);
}
/**
* 转为参数字符串
* @param paramMap 参数Map
* @return 参数字符串
*/
public static String toParamString(Map
© 2015 - 2024 Weber Informatics LLC | Privacy Policy