package com.mini.mybigscreen.utils; import java.util.Random; public class KeyUtil { public static String ObjKey(int length, int type) { Random random = new Random(); StringBuffer key = new StringBuffer(); if (type == 1) { String str = "0123456789"; for (int i = 0; i < length; ++i) { //从62个的数字或字母中选择 int number = random.nextInt(10); //将产生的数字通过length次承载到key中 key.append(str.charAt(number)); } return key.toString(); } else if (type == 2) { String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; for (int i = 0; i < length; ++i) { //从62个的数字或字母中选择 int number = random.nextInt(36); //将产生的数字通过length次承载到key中 key.append(str.charAt(number)); } return key.toString(); } else if (type == 3) { String str = "abcdefghijklmnopqrstuvwxyz0123456789"; for (int i = 0; i < length; ++i) { //从62个的数字或字母中选择 int number = random.nextInt(36); //将产生的数字通过length次承载到key中 key.append(str.charAt(number)); } return key.toString(); } else { String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for (int i = 0; i < length; ++i) { //从62个的数字或字母中选择 int number = random.nextInt(62); //将产生的数字通过length次承载到key中 key.append(str.charAt(number)); } return key.toString(); } } }