110 lines
2.8 KiB
Java
110 lines
2.8 KiB
Java
package com.foxinmy.weixin4j.util;
|
|
|
|
import java.io.UnsupportedEncodingException;
|
|
import java.net.URLEncoder;
|
|
import java.util.Comparator;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
import java.util.TreeMap;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.http.Consts;
|
|
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.TypeReference;
|
|
|
|
/**
|
|
* 签名工具类
|
|
*
|
|
* @className MapUtil
|
|
* @author jy
|
|
* @date 2014年10月31日
|
|
* @since JDK 1.7
|
|
* @see
|
|
*/
|
|
public class MapUtil {
|
|
/**
|
|
* 连接字符串
|
|
* @param object 对象
|
|
* @param encoder 是否编码
|
|
* @param lowerCase 是否转换小写
|
|
* @param extra 附加对象
|
|
* @return
|
|
*/
|
|
public static String toJoinString(Object object, boolean encoder,
|
|
boolean lowerCase, Map<String, String> extra) {
|
|
String text = JSON.toJSONString(object);
|
|
Map<String, String> map = new TreeMap<String, String>(
|
|
new Comparator<String>() {
|
|
@Override
|
|
public int compare(String o1, String o2) {
|
|
return o1.compareTo(o2);
|
|
}
|
|
});
|
|
|
|
map.putAll(JSON.parseObject(text,
|
|
new TypeReference<Map<String, String>>() {
|
|
}));
|
|
if (extra != null && !extra.isEmpty()) {
|
|
map.putAll(extra);
|
|
}
|
|
return toJoinString(map, encoder, lowerCase);
|
|
}
|
|
|
|
/**
|
|
* 连接字符串
|
|
* @param map 对象
|
|
* @param encoder 是否编码
|
|
* @param lowerCase 是否转换小写
|
|
* @return
|
|
*/
|
|
public static String toJoinString(Map<String, String> map, boolean encoder,
|
|
boolean lowerCase) {
|
|
StringBuilder sb = new StringBuilder();
|
|
Set<Map.Entry<String, String>> set = map.entrySet();
|
|
try {
|
|
if (encoder && lowerCase) {
|
|
for (Map.Entry<String, String> entry : set) {
|
|
if (StringUtils.isBlank(entry.getValue())) {
|
|
continue;
|
|
}
|
|
sb.append(entry.getKey().toLowerCase())
|
|
.append("=")
|
|
.append(URLEncoder.encode(entry.getValue(),
|
|
Consts.UTF_8.name())).append("&");
|
|
}
|
|
} else if (encoder) {
|
|
for (Map.Entry<String, String> entry : set) {
|
|
if (StringUtils.isBlank(entry.getValue())) {
|
|
continue;
|
|
}
|
|
sb.append(entry.getKey())
|
|
.append("=")
|
|
.append(URLEncoder.encode(entry.getValue(),
|
|
Consts.UTF_8.name())).append("&");
|
|
}
|
|
} else if (lowerCase) {
|
|
for (Map.Entry<String, String> entry : set) {
|
|
if (StringUtils.isBlank(entry.getValue())) {
|
|
continue;
|
|
}
|
|
sb.append(entry.getKey().toLowerCase()).append("=")
|
|
.append(entry.getValue()).append("&");
|
|
}
|
|
} else {
|
|
for (Map.Entry<String, String> entry : set) {
|
|
if (StringUtils.isBlank(entry.getValue())) {
|
|
continue;
|
|
}
|
|
sb.append(entry.getKey()).append("=")
|
|
.append(entry.getValue()).append("&");
|
|
}
|
|
}
|
|
} catch (UnsupportedEncodingException e) {
|
|
;
|
|
}
|
|
sb.deleteCharAt(sb.length() - 1);
|
|
return sb.toString();
|
|
}
|
|
}
|