常用工具类

MD5Util

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class MD5Util {
/** 全局数组 **/
private final static String[] strDigits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D","E", "F" };
/**
* 返回形式为数字跟字符串
* @param bByte
* @return
*/
private static String byteToArrayString(byte bByte) {
int iRet = bByte;
if (iRet < 0) {
iRet += 256;
}
int iD1 = iRet / 16;
int iD2 = iRet % 16;
return strDigits[iD1] + strDigits[iD2];
}
/**
* 转换字节数组为16进制字串
* @param bByte
* @return
*/
private static String byteToString(byte[] bByte) {
StringBuffer sBuffer = new StringBuffer();
for (int i = 0; i < bByte.length; i++) {
sBuffer.append(byteToArrayString(bByte[i]));
}
return sBuffer.toString();
}

public static String encode(String str) throws NoSuchAlgorithmException {
String result = null;
result = new String(str);
MessageDigest md = MessageDigest.getInstance("MD5");
result = byteToString(md.digest(str.getBytes()));
return result;
}

public static String encode(String plaintext,String encoding)
throws NoSuchAlgorithmException, UnsupportedEncodingException {
byte[] hash;
hash = MessageDigest.getInstance("MD5").digest(plaintext.getBytes(encoding));
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
}
}

Base64Util

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class Base64Util {

public static String encoder(String plaintext, String encoding) throws UnsupportedEncodingException {
final Base64.Encoder encoder = Base64.getEncoder();
String strEncoder = null;

strEncoder = encoder.encodeToString(plaintext.getBytes(encoding));
return strEncoder;
}

public static String decoder(String plaintext, String encoding) throws UnsupportedEncodingException {
final Base64.Decoder decoder = Base64.getDecoder();
String strEncoder = null;
strEncoder = new String(decoder.decode(plaintext), encoding);
return strEncoder;
}
}

ListUtil

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//ListUtil.getIdList(Trade.class,List,"id") 来获取对象中的元素

public abstract class ListUtil {

public static <T,D> List<T> listCover (Class<T> clazz, List<D> list){
List<T> voList = new ArrayList<>();
if(!CollectionUtils.isEmpty(list)){
for (D d : list) {
try {
T t = clazz.newInstance();
BeanUtils.copyProperties(d, t);
voList.add(t);
} catch (Exception e) {
throw new BusinessException("listCover数据类型转换出错");
}
}
}
return voList;
}


/**
* 提取对象数组的ID数组
* @param classz
* @param list
* @param <T>
* @return
*/
public static <T> List<String> getIdList (Class<T> classz,List<T> list){
List<String> idList = new ArrayList<>();
try{
Method method = classz.getMethod("getId");
for(T t:list){
String id = (String)method.invoke(t);
idList.add(id);
}
}catch (Exception e){
throw new BusinessException("获取ID数组出错");
}
return idList;
}

/**
* 提取对象数组的ID数组
* @param classz
* @param list
* @param <T>
* @return
*/
public static <T> List<String> getIdList (Class<T> classz, Collection<T> list, String idField){
List<String> idList = new ArrayList<>();
if(null != list && list.size() > 0){
try{
if(ObjectUtil.isNotNull(idField)){
char[] cs = idField.toCharArray();
cs[0]-=32;
Method method = classz.getMethod("get" + String.valueOf(cs));
for(T t:list){
String id = (String)method.invoke(t);
idList.add(id);
}
}

}catch (Exception e){
throw new BusinessException("获取ID数组出错");
}
}
return idList;
}

/**
* 字符串转换List<Long>
* @param str
* @return
*/
public static List<Long> stringCoverLongList(String str){
List<Long> idList = new ArrayList<>();
if(commaLinksCheck(str)){
String [] strIds = StringUtils.split(str,",");
for(String strId:strIds){
idList.add(Long.parseLong(strId));
}
}
return idList;
}

/**
* 字符串转换List<String>
* @param str
* @return
*/
public static List<String> commaLinksToList(String str){
List<String> stringList = new ArrayList<>();
if(commaLinksCheck(str)){
String [] strIds = StringUtils.split(str,",");
for(String strId:strIds){
stringList.add(strId);
}
}
return stringList;
}

/**
* 检查是否标准的:1,2,3,4,5的字符串拼接
* @param content
* @return
*/
public static boolean commaLinksCheck(String content){
boolean bool = false;
if(!StringUtils.isEmpty(content)){
bool = Pattern.matches("^(\\w+,)*\\w+$", content);
}
return bool;
}

public static String listCoverString(List<String> temp, String split) {
StringBuilder sb = new StringBuilder();
for (String tmp : temp) {
sb.append(tmp).append(split);
}
return sb.toString();
}
}