dhz
2022-06-22 06856202544f4324e27896e8a7b2fcf1298f5c68
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
package cn.ksource.core.util;
 
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
import javax.imageio.ImageIO;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
 
import org.apache.commons.lang.StringUtils;
 
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
 
import com.thoughtworks.xstream.core.util.Base64Encoder;
 
public class StringUtil {
 
    public static String test(){
        return StringUtil.getUUID();
    }
    
    
    /**
     * 验证是否为Email格式
     * @param email
     * @return
     * @version V1.0.0
     * @author 杨凯
     * @date Sep 7, 2013 12:06:34 PM
     */
    public static boolean isEmail(String email){
        Pattern pattern = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");//复杂匹配   
        Matcher matcher = pattern.matcher(email);
        if (matcher.matches()){
            return true;
        }
        return false;
    }
    
    /**
     * 
     * @param param
     * @return
     */
    public static String asc2gbk(String param){
        String ret = "";
        if (StringUtils.isNotBlank(param)) {
            try {
                ret = new String(param.getBytes("ISO-8859-1"),"GBK").trim();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                ret ="";
            }
        }
        return ret;
    }
    public static String asc2utf8(String param){
        String ret = "";
        if (StringUtils.isNotBlank(param)) {
            try {
                ret = new String(param.getBytes("ISO-8859-1"),"UTF-8").trim();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                ret ="";
            }
        }
        return ret;
    }
    /**
     * 功能描述:阿拉伯数字转汉字<BR>
     * 
     * @param a
     * @return
     * @author:杨凯<BR>
     *            时间:Apr 25, 2009 7:10:23 PM<BR>
     */
    public static String translateNumToChinese(int a) {
        String[] units = { "", "十", "百", "千", "万", "十", "百", "千", "亿" };
        String[] nums = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
        String result = "";
        if (a < 0) {
            result = "负";
            a = Math.abs(a);
        }
        String t = String.valueOf(a);
        for (int i = t.length() - 1; i >= 0; i--) {
            int r = (int) (a / Math.pow(10, i));
            if (r % 10 != 0) {
                String s = String.valueOf(r);
                String l = s.substring(s.length() - 1, s.length());
                result += nums[Integer.parseInt(l) - 1];
                result += (units[i]);
            } else {
                if (!result.endsWith("零")) {
                    result += "零";
                }
            }
        }
        return result;
    }
 
    /**
     * 功能描述:取得32位UUID<BR>
     * 
     * @return
     * @author:杨凯<BR>
     *            时间:Feb 20, 2009 11:04:36 PM<BR>
     */
    public static String getUUID() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }
 
 
    /**
     * 功能描述:电话号码用××代替<BR>
     * 
     * @param photo
     * @return
     * @author:杨凯<BR>
     *            时间:Apr 25, 2009 6:54:11 PM<BR>
     */
    public static String dhhmreplaceByXX(String photo) {
        if (org.apache.commons.lang.StringUtils.isBlank(photo)) {
            return null;
        }
        char[] strs = photo.toCharArray();
        for (int i = 3; i < 8; i++) {
            strs[i] = '*';
        }
        return new String(strs);
    }
 
    /**
     * 去掉字符串中的回车换行符
     * 
     * @param str
     * @return
     */
    public static String removeNewline(String str) {
        String result = null;
        if (str != null) {
            result = str.replaceAll("\\r\\n", "");
            result = result.replaceAll(new String(new char[] { 10 }), "");
        }
        return result;
    }
 
    /**
     * 功能描述:判断字符串是否为汉字<BR>
     * @param str
     * @return
     * @author:杨凯<BR>
     * 时间:Jul 8, 2009 9:02:48 AM<BR>
     */
    public static boolean isNumber(String str) {
        if (str.matches("\\d*")) {
            return true;
        } else {
            return false;
        }
 
    }
 
    /**
     * 功能描述:浮点型判断
     * 
     * @param str
     * @return
     * @author 胡晓<BR>
     *         时间:2009-7-29<BR>
     */
    public static boolean isFloat(String str) {
        if (str == null || "".equals(str))
            return false;
        Pattern pattern = Pattern.compile("[0-9]*(\\.?)[0-9]*");
        return pattern.matcher(str).matches();
    }
 
    /**
     * 功能描述:整型判断
     * 
     * @param str
     * @return
     * @author 胡晓<BR>
     *         时间:2009-7-29<BR>
     */
    public static boolean isInteger(String str) {
        if (StringUtil.isBlank(str))
            return false;
        return str.matches("^-?\\d+");
    }
 
    /**
     * 功能描述:获取子字符串的个数
     * 
     * @return
     * @author 胡晓<BR>
     *         时间:2009-7-16<BR>
     */
    public static int getSubCount(String sub, String string) {
        if (StringUtils.isBlank(string)) {
            return 0;
        }
        int subLength = sub.length();
        int forLength = string.length() - sub.length() + 1; // 循环次数
        int count = 0; // 记录循环次数
        for (int i = 0; i < forLength; i++) {
            if (StringUtils.substring(string, i, i + subLength).equals(sub)) {
                count++;
            }
        }
        return count;
    }
    
    /**
     * 功能描述:接受一个List<String>,如果有任何一个为空,则返回true,否则返回false
     * @param list
     * @return
     * @author 胡晓<BR>
     * 时间:2009-8-8<BR>
     */
    public static boolean isBlank(String... list){
        for (String string : list) {
            if(StringUtils.isBlank(string)){
                return true;
            }
        }
        return false;
    }
    
    
    /**
     * 如果IsNull,就返回defalut
     * @param str
     * @param defalut
     * @return
     * @version V1.0.0
     * @author 杨凯
     * @date Sep 19, 2013 8:59:22 AM
     */
    public static String defaultIfNull(String str,String defalut){
        if (StringUtils.isBlank(str)) {
            return defalut;
        }
        return str;
    }
    
    /**
     * 功能描述:都不为空,则返回true
     * @param list
     * @return
     * @author 胡晓<BR>
     * 时间:2009-9-25 上午09:04:47<BR>
     */
    public static boolean isNotBlank(String... list){
        return !isBlank(list);
    }
    
    /**
     * 功能描述:trim从前台获取的参数,包括null变成"",'变成''<BR>
     * @param s
     * @return
     * @author 胡晓<BR>
     * 时间:2009-10-28 下午03:00:52<BR>
     */
    public static String trimParam(Object s){
        String str = ConvertUtil.obj2Str(s);
        if(StringUtils.isBlank(str)){
            return "";
        }else{
            return str.replaceAll("'", "''").trim();
        }
    }
    
    /**
     * 描述:tapestry 比较一个字符串包含另一个字符串
     * @param o1
     * @param o2
     * @return
     * 作者:李坤
     * 时间:Sep 9, 2010
     */
    public static boolean Str1ContainsStr2(Object o1 , Object o2){
        if(ConvertUtil.obj2Str(o1).contains(ConvertUtil.obj2Str(o2))){
            return true;
        }
        return false;
    }
    
    /**
     * 功能描述:截取指定长度字符串,一个汉字占两个字节,字符和数字占用一个<BR>
     * @param value
     * @param length
     * @param flag 为真则后加"..."
     * @return
     * @author:杨凯<BR>
     * 时间:Sep 12, 2009 3:53:36 PM<BR>
     */
    public static String subStr(String value,int length,boolean flag){
        if (StringUtils.isBlank(value) || value.getBytes().length <= length) 
            return value;
        for (int i = 0; i <= value.length(); i++) {
            if (value.substring(0, i).getBytes().length > length) {
                if (i - 1 >= 0) {
                    return value.substring(0, i - 1)+(flag ? "...":"");
                }
            }
        }
        return value;
    }
    /**
     * 清除一个时间格式字符串中的"-" ":"和空格,返回一个格式化的String<br>
     * 例如:将一个 2010-11-13 12:00 转换为 201011131200 <br>
     * 
     * 作者:<a href="mailto:winhunter@163.com">郝世博</a><br>
     * 时间:2010-12-08 18:02
     * 
     * @param date
     * @return
     */
    public static String clearDateStrToLong(String date) throws NumberFormatException{
        String ret = "";
        if(date!=null) {
            ret = date.replaceAll("[-]*[\\s]*[:]*", "");
        }
        return ret;
    }
    /**
     * 描述:小数格式化 
     * @param value  (需要格式化的数字)
     * @param length (保留几位小数)
     * @return
     * 作者:李坤
     * 时间:Nov 5, 2010
     */
    public static Double getDoubleFormat(Double value,Integer length){
        String str=".";
        for (int i = 0; i < length; i++) {
            str+="#";
        }
        return  Double.valueOf(new DecimalFormat(str).format(value));
    }
    
    public static  String decode(String str){
        String string = "";
        if(StringUtil.isNotBlank(str)){
            try {
                string = URLDecoder.decode(str, "gbk");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return  string;
    }
    
    public static  String decode_utf(String str){
        String string = "";
        if(StringUtil.isNotBlank(str)){
            try {
                string = URLDecoder.decode(str, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return  string;
    }
    
    public static String decode_Iso_utf(String str) throws UnsupportedEncodingException{
        if(StringUtil.isBlank(str)){
            return str;
        }
        return new String(str.getBytes("ISO-8859-1"),"GBK");
    }
    
    /**
     *  将2010-1-1 转为 20100101.
     * @author <a href="mailto:winhunter@163.com">郝世博</a>
     * @time Apr 8, 2011 6:48:15 PM
     */
    public static String datestr2long(String date){
        String[] time = date.split("-");
        StringBuffer dateBuf = new StringBuffer();
        for (int i = 0; i < time.length; i++) {
            if (i==0) {
                dateBuf.append(time[i]);
            }else {
                dateBuf.append(time[i].length()<2?"0"+time[i]:time[i]);
            }
        }
        return dateBuf.toString();
    }
    
    /**
     *  将一个字符串根据给定的长度分隔成N个字符串的数组.
     * @author <a href="mailto:winhunter@163.com">郝世博</a>
     * @time Mar 30, 2011 11:06:59 PM
     */
    public static String[] splitForLength(String str , int length){
        int len = str.length();
        int pageSize = (int) Math.ceil(len/70D);
        String[]  array = new String[pageSize];
        for (int i = 0; i < pageSize; i++) {
            int begin = i*70;
            int end = ( begin+70) >= len ? len : (begin+70 );
            array[i] = str.substring( begin, end);
        }
        return array;
    }
    
    
    /**
     * 根据一个字符数组组成一个由指定字符分隔的字符串.
     * @author <a href="mailto:winhunter@163.com">郝世博</a>
     * @time Mar 31, 2011 1:42:17 PM
     */
    public static String strArrayToStr(String[] arr,String sign){
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < arr.length; i++) {
            buf.append(arr[i]);
            if ( i<arr.length-1 ) {
                buf.append(sign);
            }
        }
        return buf.toString();
    }
    
 
    /**
     *  将一个字符串中的空格和换行转行成html代码.
     * @author <a href="mailto:winhunter@163.com">郝世博</a>
     * @time Apr 8, 2011 1:46:00 PM
     */
    public static String str2Html(String str){
        String retStr = "";
        if(str!=null){
            retStr = str;
            retStr = retStr.replace("&", "&amp;");
            retStr = retStr.replace(" ", "&nbsp;");
            retStr = retStr.replace("<", "&lt;");
            retStr = retStr.replace(">", "&gt;");
            retStr = retStr.replaceAll(" ", "&nbsp;");
            retStr = retStr.replaceAll("\r\n", "<br>");
            retStr = retStr.replaceAll("\r", "<br>");
        }
        return retStr;
    }
    
    /**
     *  .
     * @author <a href="mailto:winhunter@163.com">郝世博</a>
     * @time Jun 23, 2011 6:40:22 PM
     */
    public static String num2Weekstr(int num){
        String result = "";
         switch (num) {
            case 1:
                result = "一";
                break;
            case 2:
                result = "二";
                break;
            case 3:
                result = "三";
                break;
            case 4:
                result = "四";
                break;
            case 5:
                result = "五";
                break;
            case 6:
                result = "六";
                break;
            case 7:
                result = "日";
                break;
            default:
                break;
        }
         return result;
    }
    
    /**
     *  将字符串或路径中的 \ 转为  /.
     * @author <a href="mailto:winhunter@163.com">郝世博</a>
     * @time Jul 8, 2011 3:11:54 PM
     */
    public  static String convert(String text){
        StringBuffer buf = new StringBuffer("");
        if ( null==text ) {
            return null;
        }
        char[] chars = text.toCharArray();
        for ( char tmp : chars ) {
            if (tmp=='\\') {
                tmp = '/';
            }
            buf.append(tmp);
        }
    
        return buf.toString();
    }
    /**
     * 编码
     * 描述:
     * @param str
     * @return
     * 作者:李坤
     * 时间:Aug 23, 2011
     */
    public  static String base64Encode(String str){
        if(StringUtil.isBlank(str))
            return str;
        return new Base64Encoder().encode(str.getBytes());
    }
    /**
     * 解码
     * 描述:
     * @param str
     * @return
     * @throws IOException
     * 作者:李坤
     * 时间:Aug 23, 2011
     */
    public  static String base64Decode(String str) throws IOException{
        if(StringUtil.isBlank(str))
            return str;
        return new String(new BASE64Decoder().decodeBuffer(str));
    }
    /**
     * 获取随机数
     */
    public  static String getRandom(int length) {
        String yzm = StringUtils.right(Math.random()+"", length);
        while (yzm.length()!=length) {
            yzm = StringUtils.right(Math.random()+"", length);
        }
        return yzm;
    }
    
    
    
    
    
    public  static String trim(String str) {
         if(StringUtil.isBlank(str)){
             return str;
         }
         return str.trim();
    }
    public static String[]  split(String str, String regex) {
        return str.split(regex);
    }
    public static String  replace(String str, String regex,String replacement) {
        return str.replaceAll(regex, replacement);
    }
    /**
     * 判断一个自己数组组成的字符串是否包含字符串
     * @param strs 父字符串
     * @param reg 用来拆分父字符串成为数组
     * @param str 子字符串
     * @return
     */
    public static Boolean arrayContainsStr(String strs,String reg,String str) {
        if(isBlank(strs)){
            return false;
        }
        String array []  = strs.split(reg);
        for (String string : array) {
            if(string.equals(str)){
                return true;
            }
        }
        return false;
    }
    /**
     * 获取当前工程路径
     * @return
     */
    public static String getProjectPath() {
        return StringUtil.class.getResource("/").getPath().substring(1).split("WEB-INF")[0];
    }
    
    
    public static String getStringValue(String inStr,int pos){
        String retStr="";
        if(inStr!=null&!"".equals(inStr)){
            String[] tempS=inStr.split(",");
             retStr= tempS[pos];
        }
        
        return retStr;
    }
    public static boolean isEmpty(String str){
        if(null==str||str.trim().equals("")||"null".equals(str)){
            return true;
        }
        return false;
    }
    /**
     * 分割字符串中间加字符
     */
    public static String getStringSplitByStr(String inStr,String str,int j){
        if(inStr!=null&!"".equals(inStr)&str!=null&!"".equals(str)){
            StringBuilder sb=new StringBuilder(inStr);
            int length=sb.length();
            for(int i=1;i<=length/j;i++)
            {
                if(i==length/j  &&  (length%j)==0);
                else
                    sb.insert(i*j+i-1,str);
            }
            inStr = sb.toString();
        }
        
        return inStr;
        
    }
    
    /**
     * 判断是否为空
     */
    public static boolean notEmpty(String str) {
        boolean b = false;
        if(null != str && !"".equals(str.trim())) {
            b = true;
        }
        return b;
    }
    
    
    public static boolean notEmptyNum(String str) {
        boolean b = false;
        if(null != str && !"".equals(str.trim()) && StringUtils.isNumeric(str)) {
            b = true;
        }
        return b;
    }
    
    
    /**
     * 判断是否是正确的手机号码
     * @param mobile
     * @return
     */
    public static boolean isValidMobileNum(String mobile){
        if(StringUtil.notEmptyNum(mobile)) {
            mobile = mobile.trim();
        }
        Pattern pattern = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");     
        Matcher matcher = pattern.matcher(mobile);
        return matcher.matches();
    }
    
    /**
     * 判断是否是正确的电话号码(固定电话)
     * @param mobile
     * @return
     */
    public static boolean isValidPhone(String phone){
        if(StringUtil.notEmptyNum(phone)) {
            phone = phone.trim();
        }
        Pattern pattern = Pattern.compile("^(([0\\+]\\d{2,3}-)?(0\\d{2,3})-)?(\\d{7,8})(-(\\d{3,}))?$");     
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }
    
    /**
     * 判断是否为正确的邮政编码
     */
    public static boolean isPostCode(String postCode) {
        if(StringUtil.notEmptyNum(postCode)) {
            postCode = postCode.trim();
        }
        Pattern pattern = Pattern.compile("^\\d{6}$");     
        Matcher matcher = pattern.matcher(postCode);
        return matcher.matches();
    }
    
    /**    
     * BASE64解密   
   * @param key          
     * @return          
     * @throws Exception          
     */              
    public static String decryptBASE64(String key) {               
        try {
            return new String((new BASE64Decoder()).decodeBuffer(key));
        } catch (IOException e) {
            e.printStackTrace();
        } 
        return null;
    }               
                  
    /**         
     * BASE64加密   
   * @param key          
     * @return          
     * @throws Exception          
     */              
    public static String encryptBASE64(String key)  {               
        String v = new BASE64Encoder().encodeBuffer(key.getBytes());
        return v.replaceAll("\n", "").replaceAll("\r", "");
    }  
    
    
    /**
     * 判断长度
     */
    public static boolean isRightLength(String key,int minLength,int maxLength) {
        int length = key.trim().length();
        if(length>=minLength && length<=maxLength) {
            return true;
        }
        return false;
    }
    
    /**
     * 判断是否是密码(由6-18位的字母,数字,下划线组成)
     * @param args password
     */
    public static boolean isPasswordReg(String password) {
        String passwordReg = "^\\w{6,18}$";
         Pattern pattern = Pattern.compile(passwordReg);  
         return pattern.matcher(password).matches();  
    }
    
    
    /**
     * 生成任意长度数字
     * @param len 长度
     * @return number 
     */
    public static String randomNumber(int len){
        int length = len<=0?6:len;// 随机密码长度为
        StringBuffer buffer = new StringBuffer("0123456789");
        StringBuffer sb = new StringBuffer();
        Random r = new Random();
        int range = buffer.length();
        //在此控制,随机数字的第一位不能是0,因为有的随机码是需要传给前台JS的,JS获取到首位为0的数字会自动将0去掉,这样就和后台对应不上,所以在此直接将首位为0的情况排除
        for (boolean firstZero=true;sb.length()<length;) {
            int nextInt = Integer.valueOf(String.valueOf(buffer.charAt(r.nextInt(range))));
            if(firstZero&&nextInt==0){
                continue;
            }else{
                firstZero=false;
                sb.append(nextInt);
            }
        }
        return sb.toString();
    }
 
 
    /**
     * 将cookie封装到Map里面
     * @param request
     * @return
     */
    public static Map<String,Cookie> readCookieMap(HttpServletRequest request){ 
        Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
        Cookie[] cookies = request.getCookies();
        if(null!=cookies){
            for(Cookie cookie : cookies){
                cookieMap.put(cookie.getName(), cookie);
            }
        }
        return cookieMap;
    }
    
    /**
     * 转化日期格式的形式 如:yyyy年MM月 转化为 yyyyMM
     * @param oldFormat 老的日期格式
     * @param newFormat 新的日期格式
     * @param dateStr    日期数据
     * @return
     */
    public static String formatDate(String oldFormat,String newFormat,String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat(oldFormat);
        try {
            Date d = format.parse(dateStr);
            SimpleDateFormat nFormat = new SimpleDateFormat(newFormat);
            String result = nFormat.format(d);
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 获取图片长和宽
     * @param url 图片链接
     * @return
     */
    public static int[] getImgSize(String url) {
        int[] result = new int[2];
        
        File picture = new File(url);  
        BufferedImage sourceImg;
        try {
            sourceImg = ImageIO.read(new FileInputStream(picture));
            int width = sourceImg.getWidth();
            int height = sourceImg.getHeight();
            System.out.println(width);  
            System.out.println(height);
            result[0] = width;
            result[1] = height;
            return result;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 比较两个日期的大小
     * @param beginTime 开始时间
     * @param endTime 结束时间
     * @param formatStr 日期格式
     * @return
     */
    public static boolean compareDate(String beginTime,String endTime,String formatStr) {
        boolean b = false;
        if(StringUtil.notEmpty(beginTime) && StringUtil.notEmpty(endTime)) {
            SimpleDateFormat format = new SimpleDateFormat(formatStr);//设置日期格式
            try {
                Date beginDate = format.parse(beginTime);
                Date endDate = format.parse(endTime);
                if(endDate.after(beginDate)) {
                    b = true;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        return b;
    }
    /**
     * 通过文件后缀获取文件对应的图片
     */
    public static String docSuffer(String suffer) {
        suffer = suffer.toLowerCase();
        if(suffer.equals("doc") || suffer.equals("docx")) {
            return "doc";
        } else if(suffer.equals("xls") || suffer.equals("xlsx")) {
            return "excel";
        } else if(suffer.equals("jpg") || suffer.equals("gif") || suffer.equals("jpeg") || suffer.equals("png")|| suffer.equals("bmp")) {
            return "img";
        } else if(suffer.equals("pdf")) {
            return "pdf";
        } else if(suffer.equals("ppt") || suffer.equals("pptx")) {
            return "ppt";
        } else if(suffer.equals("rar") || suffer.equals("zip")) {
            return "rar";
        } else if(suffer.equals("txt")) {
            return "txt";
        } else {
            return "cloud";
        }
    }
    /**
     * 得到文件转化后的大小
     * @param fs 文件大小 (单位B)
     */
    public static String getFileSize(double fs) {
        String fileSize = new String();
        if(fs<1024) {
            fileSize = fs+" B";
        } else if(fs>=1024 && fs<1024*1024) {
            double fsize = NumberUtil.div(fs, Double.valueOf("1024"), 0);
            fileSize = ConvertUtil.obj2Int(fsize)+" KB";
        } else {
            double size = 1024*1024;
            double fsize = NumberUtil.div(fs, size, 0);
            fileSize = ConvertUtil.obj2Int(fsize)+" MB";
        }
        return fileSize;
    }
    
    /**
     * 微信端文件后缀对应的图片名称
     */
    public static String wechateDocSuffer(String suffer) {
        suffer = suffer.toLowerCase();
        if(suffer.equals("doc") || suffer.equals("docx")) {
            return "doc";
        } else if(suffer.equals("xls") || suffer.equals("xlsx")) {
            return "xls";
        } else if(suffer.equals("jpg") || suffer.equals("gif") || suffer.equals("jpeg") || suffer.equals("png")|| suffer.equals("bmp")) {
            return "img";
        } else if(suffer.equals("pdf")) {
            return "pdf";
        } else if(suffer.equals("ppt") || suffer.equals("pptx")) {
            return "ppt";
        } else if(suffer.equals("rar") || suffer.equals("zip")) {
            return "rar";
        } else if(suffer.equals("txt")) {
            return "txt";
        } else {
            return "default";
        }
    }
    /**
     * 详情页对应的图片名称
     */
    public static String detailDocSuffer(String suffer) {
        suffer = suffer.toLowerCase();
        if(suffer.equals("doc") || suffer.equals("docx")) {
            return "DOC2";
        } else if(suffer.equals("xls") || suffer.equals("xlsx")) {
            return "XLS2";
        } else if(suffer.equals("jpg") || suffer.equals("gif") || suffer.equals("jpeg") || suffer.equals("png")|| suffer.equals("bmp")) {
            return "JPG2";
        } else if(suffer.equals("pdf")) {
            return "PDF2";
        } else if(suffer.equals("ppt") || suffer.equals("pptx")) {
            return "PPT2";
        } else if(suffer.equals("rar") || suffer.equals("zip")) {
            return "Archive2";
        } else if(suffer.equals("txt")) {
            return "TXT2";
        } else {
            return "common1_1";
        }
    }
    /**
     * 通过年,月获得该月的第一天和最后一天  
     * @param year 格式 yyyy 
     * @param month  格式 MM
     * @return  数组第一个当前月第一天, 数组第二个:当前月最后一天
     */
    public static String[] queryFirstLastDate(String year,String month) {
        String[] str = new String[2];
        if(!StringUtil.notEmptyNum(year)||year.length()!=4||Integer.valueOf(year)<0) {
            year = DateUtil.format(new Date(), "yyyy");
        }
        if(!StringUtil.notEmptyNum(month)||Integer.valueOf(month)<1||Integer.valueOf(month)>12) {
            month = DateUtil.format(new Date(), "MM");
        }
        if(month.length()!=2) {
            month = "0"+month;
        }
        String firstDate = year+"-"+month+"-01";
        str[0] = firstDate;
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date date = dateFormat.parse(firstDate);
            String lastDate = dateFormat.format(lastDayOfMonth(date));
            str[1] = lastDate;
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return str;
    }
    
    /*
     * 获得指定日期所在月最后一天
     */
     public static Date lastDayOfMonth(Date date) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(date);
            cal.set(Calendar.DAY_OF_MONTH, 1);
            cal.roll(Calendar.DAY_OF_MONTH, -1);
            return cal.getTime();
     }
    
    
}