Java验证码字符串生成

来源:互联网 发布:小灰灰淘宝买家采集器 编辑:程序博客网 时间:2024/06/07 03:12
import java.util.Random;/** *  * <p>验证码字符串生成</p> * @version V3.1 */public class VerificationCode {    public static final String SEEDS = "23456789ABCDEFGHJKMNPQRSTUVWXYZ";//去除0 1 I L等易混淆的字母数字    public static final int NORMAL_SIZE = 4;    private static Random random = new Random();    public static String getVerificationCode(){        return getVerificationCode(NORMAL_SIZE);    }    public static String getVerificationCode(int verificationSize){        return getVerificationCode(verificationSize, SEEDS);    }    public static String getVerificationCode(int verificationSize,String seeds){        seeds = seedsIsNullOrEmpty(seeds);        int maxIndex = seeds.length();        StringBuffer verificationCode = new StringBuffer(verificationSize);        for(int i=0;i<verificationSize;i++){            int randomIndex = random.nextInt(maxIndex);            char seed = seeds.charAt(randomIndex);            verificationCode.append(seed);        }        return verificationCode.toString();    }    public static String seedsIsNullOrEmpty(String seeds){        if (seeds == null || "".equals(seeds)) {            seeds = SEEDS;        }        return seeds;    }}
0 0