java 验证码生成工具详解编程语言

/** 
 * 验证码 
 * @author joefany 
 */ 
public class CodeUtil { 
	 
	/** 
	 * 获取验证码 
	 */ 
	public static String code() throws Exception { 
		char[] code = new char[] {'0', '1', '2', '3', '4', '5', '6','7', '8', '9'}; 
		Random random = new Random(); 
		 
		String sRand = ""; 
		for (int i = 0; i < 4; i++) { 
			String rand = String.valueOf(code[random.nextInt(code.length)]); 
			sRand += rand; 
		} 
		return sRand; 
	} 
	public static String code(int len) { 
//		char[] code = new char[] {'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'}; 
		char[] code = new char[] {'0', '1', '2', '3', '4', '5', '6','7', '8', '9'}; 
		Random random = new Random(); 
 
		String sRand = ""; 
		for (int i = 0; i < len; i++) { 
			String rand = String.valueOf(code[random.nextInt(code.length)]); 
			sRand += rand; 
		} 
		return sRand; 
	}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/13707.html

(0)
上一篇 2021年7月19日
下一篇 2021年7月19日

相关推荐

发表回复

登录后才能评论