package com. gblfy. util; import org. springframework. stereotype. Component; import java. util. ArrayList;
import java. util. Arrays;
import java. util. Random;
@Component
public class RandomStrUtil { public static enum TYPE { LETTER, CAPITAL, NUMBER, LETTER_CAPITAL, LETTER_NUMBER, CAPITAL_NUMBER, LETTER_CAPITAL_NUMBER, } private static String[ ] lowercase = { "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 String[ ] capital = { "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 String[ ] number = { "1" , "2" , "3" , "4" , "5" , "6" , "7" , "8" , "9" , "0" } ; private static Random random = new Random ( ) ; public static String getRandom ( int num, TYPE type) { ArrayList< String> temp = new ArrayList < String> ( ) ; StringBuffer code = new StringBuffer ( ) ; switch ( type) { case LETTER: temp. addAll ( Arrays. asList ( lowercase) ) ; break ; case CAPITAL: temp. addAll ( Arrays. asList ( capital) ) ; break ; case NUMBER: temp. addAll ( Arrays. asList ( number) ) ; break ; case LETTER_CAPITAL: temp. addAll ( Arrays. asList ( lowercase) ) ; temp. addAll ( Arrays. asList ( capital) ) ; break ; case LETTER_NUMBER: temp. addAll ( Arrays. asList ( lowercase) ) ; temp. addAll ( Arrays. asList ( number) ) ; break ; case CAPITAL_NUMBER: temp. addAll ( Arrays. asList ( capital) ) ; temp. addAll ( Arrays. asList ( number) ) ; break ; case LETTER_CAPITAL_NUMBER: temp. addAll ( Arrays. asList ( lowercase) ) ; temp. addAll ( Arrays. asList ( capital) ) ; temp. addAll ( Arrays. asList ( number) ) ; break ; } for ( int i = 0 ; i < num; i++ ) { code. append ( temp. get ( random. nextInt ( temp. size ( ) ) ) ) ; } return code. toString ( ) ; } public static void main ( String[ ] args) { System. out. println ( RandomStrUtil. getRandom ( 64 , RandomStrUtil. TYPE. LETTER_CAPITAL) ) ; }
}