View Javadoc
1   package com.foxinmy.weixin4j.util;
2   
3   import java.io.UnsupportedEncodingException;
4   import java.net.URLEncoder;
5   import java.util.HashMap;
6   import java.util.Map;
7   import java.util.Set;
8   import java.util.TreeMap;
9   
10  import com.alibaba.fastjson.JSONObject;
11  import com.foxinmy.weixin4j.xml.ListsuffixResultSerializer;
12  
13  /**
14   * 签名工具类
15   *
16   * @className MapUtil
17   * @author jinyu(foxinmy@gmail.com)
18   * @date 2014年10月31日
19   * @since JDK 1.6
20   * @see
21   */
22  public class MapUtil {
23  	/**
24  	 * 连接字符串
25  	 *
26  	 * @param object
27  	 *            对象
28  	 * @param encoder
29  	 *            是否编码
30  	 * @param lowerCase
31  	 *            是否转换小写
32  	 * @return
33  	 */
34  	public static String toJoinString(Object object, boolean encoder,
35  			boolean lowerCase) {
36  		Map<String, String> map = new HashMap<String, String>();
37  		JSONObject obj = null;
38  		if (object instanceof String) {
39  			obj = JSONObject.parseObject((String) object);
40  		} else {
41  			obj = ListsuffixResultSerializer.serializeToJSON(object);
42  		}
43  		for (String key : obj.keySet()) {
44  			map.put(key, obj.getString(key));
45  		}
46  		return toJoinString(map, encoder, lowerCase);
47  	}
48  
49  	/**
50  	 * 拼接字符串
51  	 *
52  	 * @param map
53  	 *            对象
54  	 * @param encoder
55  	 *            是否编码
56  	 * @param lowerCase
57  	 *            是否转换小写
58  	 * @return
59  	 */
60  	public static String toJoinString(Map<String, String> map, boolean encoder,
61  			boolean lowerCase) {
62  		map.remove("sign");
63  		map = new TreeMap<String, String>(map);
64  		StringBuilder sb = new StringBuilder();
65  		Set<Map.Entry<String, String>> set = map.entrySet();
66  		try {
67  			if (encoder && lowerCase) {
68  				for (Map.Entry<String, String> entry : set) {
69  					if (StringUtil.isBlank(entry.getValue())) {
70  						continue;
71  					}
72  					sb.append(entry.getKey().toLowerCase())
73  							.append("=")
74  							.append(URLEncoder.encode(entry.getValue(),
75  									Consts.UTF_8.name())).append("&");
76  				}
77  			} else if (encoder) {
78  				for (Map.Entry<String, String> entry : set) {
79  					if (StringUtil.isBlank(entry.getValue())) {
80  						continue;
81  					}
82  					sb.append(entry.getKey())
83  							.append("=")
84  							.append(URLEncoder.encode(entry.getValue(),
85  									Consts.UTF_8.name())).append("&");
86  				}
87  			} else if (lowerCase) {
88  				for (Map.Entry<String, String> entry : set) {
89  					if (StringUtil.isBlank(entry.getValue())) {
90  						continue;
91  					}
92  					sb.append(entry.getKey().toLowerCase()).append("=")
93  							.append(entry.getValue()).append("&");
94  				}
95  			} else {
96  				for (Map.Entry<String, String> entry : set) {
97  					if (StringUtil.isBlank(entry.getValue())) {
98  						continue;
99  					}
100 					sb.append(entry.getKey()).append("=")
101 							.append(entry.getValue()).append("&");
102 				}
103 			}
104 		} catch (UnsupportedEncodingException e) {
105 			;
106 		}
107 		sb.deleteCharAt(sb.length() - 1);
108 		return sb.toString();
109 	}
110 }