admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
package com.yeshi.buwan.util;
 
import com.google.gson.*;
 
import javax.persistence.Entity;
import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.lang.reflect.Type;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * @author Administrator
 */
@Entity
public class StringUtil {
 
    // 判断是否为电话号码
    public static boolean isMobile(String mobile) {
 
        String regex = "^((13[0-9])|(17[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(mobile);
 
        if (mobile == null || mobile.equals("") || mobile.length() != 11) {
            return false;
        } else {
            return m.find();
        }
    }
 
 
    /**
     * 连接数组内的元素
     *
     * @param srcList
     * @param seperate
     * @return
     */
    public static String join(List srcList, String seperate) {
        String st = "";
        for (int i = 0; i < srcList.size(); i++) {
            st += srcList.get(i) + seperate;
        }
        if (st.endsWith(seperate)) {
            st = st.substring(0, st.length() - seperate.length());
        }
        return st;
    }
 
 
    public static String join(Collection srcList, String seperate) {
        String st = "";
        for (Iterator<String> its = srcList.iterator(); its.hasNext(); ) {
            st += its.next() + seperate;
        }
        if (st.endsWith(seperate)) {
            st = st.substring(0, st.length() - seperate.length());
        }
        return st;
    }
 
    /**
     * 随机码生成
     *
     * @return
     */
    public static String getRandomCode() {
        String sts = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String code = "";
        for (int i = 0; i < 10; i++) {
            int p = (int) (Math.random() * 26);
            code += sts.charAt(p);
        }
        return code;
    }
 
    /**
     * 验证码生成
     *
     * @return
     */
    public static String getVerifyCode() {
        return getNumberVerifyCode(6);
    }
 
    public static String getNumberVerifyCode(int number) {
        String sts = "0123456789";
        String code = "";
        for (int i = 0; i < number; i++) {
            int p = (int) (Math.random() * 10);
            code += sts.charAt(p);
        }
        return code;
    }
 
    public static String getVerifyCode(int number) {
        String sts = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String code = "";
        for (int i = 0; i < number; i++) {
            int p = (int) (Math.random() * sts.length());
            code += sts.charAt(p);
        }
        return code;
    }
 
    // 获取短信发送的格式
    public static String getMessageStyle(String code) {
 
        return "校验码:" + code + ",用于账号注册。【请勿向任何人提供您收到的短信校验码】";
    }
 
    // 是否是空的字符串
    public static boolean isNullOrEmpty(String text) {
        if (text == null || text.length() == 0 || text.equals("null")) {
            return true;
        }
        return false;
    }
 
    // 32位MD5加密
    public static String Md5(String st) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            try {
                md.update(st.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            byte b[] = md.digest();
 
            int i;
 
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            LogUtil.i("MD5加密后的值:" + buf.toString());
            return buf.toString();
            // LogUtil.i("result: " + buf.toString());// 32位的加密
            // LogUtil.i("result: " + buf.toString().substring(8,
            // 24));// 16位的加密
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    public static String getString(String st) {
        return isNullOrEmpty(st) ? "" : st;
    }
 
    public static String getString(int i) {
        return getString(i + "");
    }
 
    public static String getString(long i) {
        return getString(i + "");
    }
 
    // 输出JSON的数据格式
 
    public static String outPutResultJson(Object o) {
        if (o instanceof String) {
            return o.toString();
        } else {
            Gson gson = new GsonBuilder().enableComplexMapKeySerialization().excludeFieldsWithoutExposeAnnotation()
                    .setDateFormat(DateFormat.LONG).registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
                        @Override
                        public JsonElement serialize(Date value, Type theType, JsonSerializationContext context) {
                            if (value == null) {
                                return new JsonPrimitive("0");
                            } else {
                                return new JsonPrimitive(value.getTime());
                            }
                        }
                    }).setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)// 会把字段首字母大写
                    .setPrettyPrinting().setVersion(1.0).create();
            String st = gson.toJson(o);
            return Utils.JsonFilter(st);
        }
    }
 
    // 输出JSON的数据格式
 
    public static JsonArray outPutResultJson(List<Object> list) {
        Gson gson = new GsonBuilder().enableComplexMapKeySerialization().excludeFieldsWithoutExposeAnnotation()
                .setDateFormat(DateFormat.LONG).setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)// 会把字段首字母大写
                .setPrettyPrinting().setVersion(1.0).create();
 
        JsonParser parser = new JsonParser();
        JsonElement el = parser.parse(gson.toJson(list).toString());
 
        // 把JsonElement对象转换成JsonObject
        JsonObject jsonObj = null;
        if (el.isJsonObject()) {
            jsonObj = el.getAsJsonObject();
        }
 
        // 把JsonElement对象转换成JsonArray
        JsonArray jsonArray = null;
        if (el.isJsonArray()) {
            jsonArray = el.getAsJsonArray();
        }
        return jsonArray;
    }
 
    /*
     * 获取中间带星号的电话号码
     */
    public static String getSubMobile(String mobile) {
        if (!StringUtil.isNullOrEmpty(mobile) && mobile.length() > 10) {
            return mobile.subSequence(0, 3).toString() + "****" + mobile.subSequence(7, mobile.length()).toString();
        } else
            return mobile;
    }
 
    public static String formatNumber(String number) {
        if (!isNullOrEmpty(number)) {
            float f = Float.parseFloat(number);
            if (f % 1.0 == 0) {
                return ((int) f + "");
            } else if (f % 0.1 == 0) {
                return (NumberUtil.get1PointNumber(f) + "");
            }
            return f + "";
        } else {
            return 0 + "";
        }
    }
 
    public static String getAbsoluteUrl(HttpServletRequest request, String url) {
        if (StringUtil.isNullOrEmpty(url))
            return "";
        if (url.startsWith("http://") || url.startsWith("https://"))
            return url;
        else
            return "http://" + Constant.WEBSITE + "/BuWan" + url;
    }
 
    public static int getPage(String page) {
        if (StringUtil.isNullOrEmpty(page)) {
            return 0;
        } else {
            try {
                return Integer.parseInt(page);
            } catch (Exception e) {
                return 0;
            }
        }
    }
 
    public static String getBase64(String str) {
        byte[] b = null;
        String s = null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = Base64.getEncoder().encodeToString(b);
        }
        return s;
    }
 
    public static String getBase64FromByte(byte[] b) {
 
        String s = null;
        if (b != null) {
            s = Base64.getEncoder().encodeToString(b);
        }
        return s;
    }
 
    // 解密
    public static String getFromBase64(String s) {
        String result = null;
        if (s != null) {
            byte[] b = Base64.getDecoder().decode(s);
            try {
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }
 
    public static byte[] getFromBase64Byte(String s) {
        return Base64.getDecoder().decode(s);
    }
 
    public static String getNumberFromString(String st) {
        String number = "";
        Pattern p = Pattern.compile("[0-9\\.]+");
        Matcher m = p.matcher(st);
 
        while (m.find()) {
            number += (m.group() + ",");
        }
        return number;
    }
 
    public static String getNotNullString(String st) {
        if (isNullOrEmpty(st))
            return "";
        else
            return st;
    }
 
    public static String getUTF8String(String resource, String chactor) {
        if (!isNullOrEmpty(resource)) {
            try {
                return new String(resource.getBytes(chactor), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return "";
    }
 
    @SuppressWarnings("restriction")
    public static boolean generateImageFromBase64(String imgStr, String imgFilePath) { // 对字节数组字符串进行Base64解码并生成图片
        if (!new File(imgFilePath).exists())
            try {
                new File(imgFilePath).createNewFile();
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        if (imgStr == null) // 图像数据为空
            return false;
        try {
            // Base64解码
            byte[] b = Base64.getDecoder().decode(imgStr);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            OutputStream out = new FileOutputStream(imgFilePath);
            out.write(b);
            out.flush();
            out.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
 
    public static boolean match(String regix, String src) {
        Pattern p = Pattern.compile(regix);
        Matcher m = p.matcher(src);
        return m.find();
    }
 
}