private String getRandKeys( int intLength ) {
String retStr = "";
String strTable = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz";
int len = strTable.length();
boolean bDone = true;
do {
retStr = "";
int count = 0;
for ( int i = 0; i < intLength; i++ ) {
double dblR = Math.random() * len;
int intR = (int) Math.floor( dblR );
char c = strTable.charAt( intR );
if ( ( '0' <= c ) && ( c <= '9' ) ) {
count++;
}
retStr += strTable.charAt( intR );
}
if ( count >= 2 ) {
bDone = false;
}
} while ( bDone );
return retStr;
}
传入8就能生成八位的随机字符串,同理可生成任意位数的随机字符串。可用于生成临时密码和图形验证码。