Tools
1 import java.io.IOException; 2 import java.security.MessageDigest; 3 import java.security.NoSuchAlgorithmException; 4 import java.util.ArrayList; 5 import java.util.HashMap; 6 import java.util.List; 7 import java.util.Map; 8 9 import org.apache.commons.lang.StringUtils; 10 import org.apache.commons.logging.Log; 11 import org.apache.commons.logging.LogFactory; 12 13 import com.fasterxml.jackson.annotation.JsonInclude.Include; 14 import com.fasterxml.jackson.core.JsonParseException; 15 import com.fasterxml.jackson.core.JsonParser; 16 import com.fasterxml.jackson.core.JsonProcessingException; 17 import com.fasterxml.jackson.databind.JsonMappingException; 18 import com.fasterxml.jackson.databind.ObjectMapper; 19 20 public class Tools { 21 22 private static final Log log = LogFactory.getLog(Tools.class); 23 private static ObjectMapper mapper = new ObjectMapper(); 24 25 static { 26 mapper.setSerializationInclusion(Include.NON_NULL); 27 mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true); 28 } 29 30 public static String md5(String str) { 31 try { 32 MessageDigest md = MessageDigest.getInstance("MD5"); 33 md.update(str.getBytes()); 34 byte digest[] = md.digest(); 35 int i; 36 StringBuffer buf = new StringBuffer(""); 37 for (int offset = 0; offset < digest.length; offset++) { 38 i = digest[offset]; 39 if (i < 0) 40 i += 256; 41 if (i < 16) 42 buf.append("0"); 43 buf.append(Integer.toHexString(i)); 44 } 45 return buf.toString(); 46 } catch (NoSuchAlgorithmException e) { 47 return null; 48 } 49 } 50 51 public static String md5(long str) { 52 try { 53 MessageDigest md = MessageDigest.getInstance("MD5"); 54 md.update((str+"").getBytes()); 55 byte digest[] = md.digest(); 56 int i; 57 StringBuffer buf = new StringBuffer(""); 58 for (int offset = 0; offset < digest.length; offset++) { 59 i = digest[offset]; 60 if (i < 0) 61 i += 256; 62 if (i < 16) 63 buf.append("0"); 64 buf.append(Integer.toHexString(i)); 65 } 66 return buf.toString(); 67 } catch (NoSuchAlgorithmException e) { 68 return null; 69 } 70 } 71 72 public static String write2JsonStr(Object o) { 73 String jsonStr = ""; 74 try { 75 jsonStr = mapper.writeValueAsString(o); 76 } catch (JsonProcessingException e) { 77 log.error("write2JsonStr() exception: " + e.getMessage()); 78 } 79 return jsonStr; 80 } 81 82 public static Object json2Object(String json, Class<?> clazz) { 83 try { 84 return mapper.readValue(json, clazz); 85 } catch (JsonParseException e) { 86 log.error("json2Object() parseException: " + e.getMessage()); 87 } catch (JsonMappingException e) { 88 log.error("json2Object() mappingException: " + e.getMessage()); 89 } catch (IOException e) { 90 log.error("json2Object() IOException: " + e.getMessage()); 91 } 92 return null; 93 } 94 95 public static Map<String, Object> json2Map(String json) { 96 try { 97 if (StringUtils.isBlank(json)) { 98 return new HashMap<String, Object>(); 99 } 100 return mapper.readValue(json, Map.class); 101 } catch (JsonParseException e) { 102 log.error("json2Map(), 出错的json内容:" + json + " ,JsonParseException: " + e.getMessage()); 103 } catch (JsonMappingException e) { 104 log.error("json2Map(), 出错的json内容:" + json + " ,JsonMappingException: " + e.getMessage()); 105 } catch (IOException e) { 106 log.error("json2Map(), 出错的json内容为:" + json + " ,IOException: " + e.getMessage()); 107 } 108 return new HashMap<String, Object>(); 109 } 110 111 public static List<Map<String, Object>> jsonArray2List(String jsonArray) { 112 try { 113 return mapper.readValue(jsonArray, List.class); 114 } catch (JsonParseException e) { 115 log.error("jsonArray2List() exception, 异常字符串: " + jsonArray, e); 116 } catch (JsonMappingException e) { 117 log.error("jsonArray2List() exception, 异常字符串: " + jsonArray, e); 118 } catch (IOException e) { 119 log.error("jsonArray2List() exception", e); 120 } 121 return new ArrayList<Map<String, Object>>(); 122 } 123 124 }