-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathGeneralUtils.java
More file actions
154 lines (133 loc) · 3.84 KB
/
GeneralUtils.java
File metadata and controls
154 lines (133 loc) · 3.84 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package com.kevin.blog.utils;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.Collection;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
/**
* 通用工具类
* @author KevinBlandy
* @version 1.0
* @date 2017年5月11日 下午9:08:21
*/
public class GeneralUtils {
private static final int[] NUMBERS = {0,1,2,3,4,5,6,7,8,9};
private static final String[] LETTERS = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t", "u","v","w","x","y","z"};
private static final String AJAX_HEADER_NAME = "x-requested-with";
private static final String AJAX_HEADER_VALUE="XMLHttpRequest";
private static final String OPTIONS_REQUEST_METHOD = "OPTIONS";
/**
* 数组是否为空
* @param t
* @param <T>
* @return
*/
public static <T> boolean isEmpty(T[] t){
return t == null || t.length == 0;
}
/**
* 字符串是否为空,或者为""
* @param string
* @return
*/
public static boolean isEmpty(String string){
return string == null || string.trim().isEmpty();
}
/**
* Set集合是否为空
* @param set
* @param <T>
* @return
*/
public static <T> boolean isEmpty(Set<T> set){
return set == null || set.isEmpty();
}
/**
* Collection是否为空
* @param collection
* @param <T>
* @return
*/
public static <T> boolean isEmpty(Collection<T> collection){
return collection == null || collection.isEmpty();
}
/**
* Map是否为空
* @param map
* @param <K>
* @param <V>
* @return
*/
public static <K,V> boolean isEmpty(Map<K,V> map){
return map == null || map.isEmpty();
}
/**
* 获取32位无符号大写UUID
* @return
*/
public static String getUUID(){
return UUID.randomUUID().toString().replace("-","").toUpperCase();
}
/**
* 获取0-9随机字符串,自定义长度
* @param length
* @return
*/
public static String getRandomNum(int length){
StringBuilder sb = new StringBuilder();
Random random = new Random();
for(int x = 0;x < length ; x++){
sb.append(NUMBERS[random.nextInt(NUMBERS.length)]);
}
return sb.toString();
}
/**
* 获取a-z随机字符串,自定义长度
* @param length
* @return
*/
public static String getRandomLetter(int length){
StringBuilder sb = new StringBuilder();
Random random = new Random();
for(int x = 0;x < length ; x++){
sb.append(LETTERS[random.nextInt(LETTERS.length)]);
}
return sb.toString();
}
/**
* 判断是否是Ajax异步请求
* @param request
* @return
*/
public static boolean isAjaxRequest(HttpServletRequest request){
String header = request.getHeader(AJAX_HEADER_NAME);
return header == null ? false : header.equalsIgnoreCase(AJAX_HEADER_VALUE);
}
/**
* 是否是OPTIONS请求
* @param request
* @return
*/
public static boolean isOptionsRequest(HttpServletRequest request) {
return request.getMethod().equalsIgnoreCase(OPTIONS_REQUEST_METHOD);
}
/**
* 对字符串进行Base64编码
* @param data
* @return
*/
public static String base64Encode(String data){
return new String(Base64.getEncoder().encode(data.getBytes(StandardCharsets.UTF_8)),StandardCharsets.UTF_8);
}
/**
* 对指定Base64编码字符串进行解码
* @param data
* @return
*/
public static String base64Decode(String data){
return new String(Base64.getDecoder().decode(data),StandardCharsets.UTF_8);
}
}