Utils.java
6.2 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
package com.teplot.common;
import java.io.File;
import java.io.FileFilter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.jfinal.kit.HashKit;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
/**
* Depiction: 常用工具方法
* <p>
* Modify:
* <p>
* Author: Kevin Lynn
* <p>
* Create Date:2016年7月28日 下午2:17:59
* <p>
*
* @version 1.0
* @since 1.0
*/
public final class Utils {
public static void main(String[] args) {
String password = HashKit.sha256("000000");
System.out.println(password);
System.out.println(password.length());
}
/**
* 加载配置文件
*/
public static void loadConfig() {
PropKit.use("config.properties");
}
/**
* 批量导入sql文件
*
* @param arp
* @param sqlBasePath
* sql文件的根目录
*/
public static void loadSqls(ActiveRecordPlugin arp, String sqlBasePath) {
File sqlBasePathDir = new File(sqlBasePath);
if (sqlBasePathDir != null && sqlBasePathDir.exists() && sqlBasePathDir.isDirectory()) {
arp.setBaseSqlTemplatePath(sqlBasePath);
File[] sqls = sqlBasePathDir.listFiles(new FileFilter() {
public boolean accept(File f) {
if (f.isFile() && f.getName().endsWith(".sql")) {
return true;
}
return false;
}
});
for (File sql : sqls) {
arp.addSqlTemplate(sql.getName());
}
}
}
public static int str2Int(String str) {
int value = 0;
try {
value = Integer.parseInt(str);
} catch (Exception e) {
}
return value;
}
public static float str2Float(String str) {
float value = 0.0f;
try {
value = Float.parseFloat(str);
} catch (Exception e) {
}
return value;
}
public static String subStr(String source, int number) {
if (StrKit.isBlank(source)) {
return "";
}
int length = source.length();
if (length >= number) {
source = source.substring(0, number);
}
return source;
}
public static int checkStock(String stock) {
int tmp = Integer.parseInt(formatNumber(stock, 0));
tmp = tmp > 9999 ? 9999 : tmp;
tmp = tmp < 0 ? 0 : tmp;
return tmp;
}
/**
* 格式化数字,为两位小数点的字符串
*
* @param number
* 数字原始数据
* @param count
* 小数点位数
* @return String
*/
public static String formatNumber(String number, int count) {
count = count >= 0 ? count : 0;
float tmp = 0.f;
try {
tmp = Float.parseFloat(number);
} catch (Exception e) {
}
DecimalFormat format = new DecimalFormat("0");
if (count > 0) {
StringBuffer pattern = new StringBuffer();
pattern.append("0.");
for (int i = 0; i < count; i++) {
pattern.append("0");
}
format = new DecimalFormat(pattern.toString());
}
return format.format(tmp);
}
/**
* 解析价格的整数位和小数位
*
* @param number
* 要解析的价格
* @return String[] index_0 整数位; index_1 小数位;
*/
public static String[] parseNumber(String number) {
String tmp = formatNumber(number, 2);
return tmp.split("\\.");
}
public static String productNo() {
return new SimpleDateFormat("yyMMddHHmmssS").format(new Date());
}
public static String orderNo() {
Random random = new Random();
String rnumber = random.nextInt(9) + "" + random.nextInt(9) + "" + random.nextInt(9) + "" + random.nextInt(9);
long time = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
return format.format(new Date(time)) + "-" + rnumber;
}
public static String uuid() {
return UUID.randomUUID().toString().replace("-", "");
}
public static String uuid(int number) {
if (number < 1) {
return null;
}
String uuid = uuid();
return uuid.substring(0, number);
}
public static String formatTime(long time) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return format.format(new Date(time));
}
public static List<String> getImgFromHtml(String html) {
if (StrKit.isBlank(html)) {
return null;
}
List<String> imgList = new ArrayList<String>();
Pattern p_image;
Matcher m_image;
String regEx_img = "(<img.*src\\s*=\\s*(.*?)[^>]*?>)";
p_image = Pattern.compile(regEx_img, Pattern.CASE_INSENSITIVE);
m_image = p_image.matcher(html);
while (m_image.find()) {
String img = m_image.group();
Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
while (m.find()) {
String tempSelected = m.group(1);
imgList.add(tempSelected);
}
}
return imgList;
}
public static String host() {
return PropKit.get("host");
}
public static String contextPathAdmin() {
return PropKit.get("contextPathAdmin");
}
public static String contextPathApi() {
return PropKit.get("contextPathApi");
}
public static String getBaseUploadDir() {
return PropKit.get("baseUploadDir");
}
public static String getPortraitDir() {
String temp = getBaseUploadDir() + "/portrait";
Utils.mkdirs(temp);
return temp;
}
public static String getImgDir() {
String temp = getBaseUploadDir() + "/img";
Utils.mkdirs(temp);
return temp;
}
public static String getQRDir() {
String temp = getBaseUploadDir() + "/qr";
Utils.mkdirs(temp);
return temp;
}
public static String getFileDir() {
String temp = getBaseUploadDir() + "/file";
Utils.mkdirs(temp);
return temp;
}
public static boolean mkdirs(String dirPath) {
boolean flag = false;
File dir = new File(dirPath);
if (!dir.exists()) {
flag = dir.mkdirs();
}
return flag;
}
public static boolean deleteFile(String filePath) {
File file = new File(filePath);
if (file.exists()) {
return file.delete();
}
return true;
}
public static void deleteDir(String dirPath) {
File dir = new File(dirPath);
if (dir.exists() && dir.isDirectory()) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.exists() && file.isFile()) {
file.delete();
} else {
deleteDir(file.getAbsolutePath());
}
}
}
}
}
}