生成长度大于6位并必须含有数字+小写字母+大写字母.
public class StringUtil { private static String [] genkey = {"0","1","2","3","4","5","6","7","8","9","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","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 [] gennum = {"0","1","2","3","4","5","6","7","8","9"}; private static String [] genup = {"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 [] gendown = {"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"}; public static String genPwd(int length) { StringBuffer sb = new StringBuffer(); Random random = new Random(); if(length<6) { throw new java.lang.IllegalArgumentException("fun StringUtil.genPwd param length must > 6"); } sb.append(gendown[random.nextInt(gendown.length)]); sb.append(genup[random.nextInt(genup.length)]); sb.append(gennum[random.nextInt(gennum.length)]); for (int i = 3; i < length; i++) { sb.append(genkey[random.nextInt(genkey.length)]); } return sb.toString(); } public static void main(String[] args) { for (int i = 0; i < 100; i++) { System.out.println(genPwd(16)); } } }