zoukankan      html  css  js  c++  java
  • 验证码产生程序

    登录的时候,经常需要输入验证码,我们来写一个验证码产生的函数,包括数字,大写字母,小写字母各种情况的组合

    VerifyCode.java

      1 package com.xiejiaohui.util;
      2 
      3 import java.util.Random;
      4 
      5 /**
      6  * 验证码产生器  2016-01-24
      7  * 
      8  * @author xiejiaohui
      9  *
     10  */
     11 public class VerifyCode {
     12 
     13     /**
     14      * 产生验证码
     15      * 
     16      * @param type
     17      * @param length
     18      * @param exChars
     19      * @return
     20      */
     21     public static String generateTextCode(int type, int length, String exChars) {
     22         if (length <= 0) {
     23             return "";
     24         }
     25         StringBuffer code = new StringBuffer();
     26         int i = 0;
     27         Random r = new Random();
     28         switch (type) {
     29         default:
     30             break;
     31         case 0: // 仅数字
     32             while (i < length) {
     33                 int t = r.nextInt(10);
     34                 if (exChars == null || exChars.indexOf((new StringBuilder(String.valueOf(t))).toString()) < 0) {
     35                     code.append(t);
     36                     i++;
     37                 }
     38             }
     39             break;
     40         case 1: // 仅字母(即大写字母、小写字母混合)
     41             while (i < length) {
     42                 int t = r.nextInt(123);
     43                 if ((t >= 97 || t >= 65 && t <= 90) && (exChars == null || exChars.indexOf((char) t) < 0)) {
     44                     code.append((char) t);
     45                     i++;
     46                 }
     47             }
     48             break;
     49         case 2: // 数字、大写字母、小写字母混合
     50             while (i < length) {
     51                 int t = r.nextInt(123);
     52                 if ((t >= 97 || t >= 65 && t <= 90 || t >= 48 && t <= 57)
     53                         && (exChars == null || exChars.indexOf((char) t) < 0)) {
     54                     code.append((char) t);
     55                     i++;
     56                 }
     57             }
     58             break;
     59         case 3: // 数字、大写字母混合
     60             while (i < length) {
     61                 int t = r.nextInt(91);
     62                 if ((t >= 65 || t >= 48 && t <= 57) && (exChars == null || exChars.indexOf((char) t) < 0)) {
     63                     code.append((char) t);
     64                     i++;
     65                 }
     66             }
     67             break;
     68         case 4: // 数字、小写字母混合
     69             while (i < length) {
     70                 int t = r.nextInt(123);
     71                 if ((t >= 97 || t >= 48 && t <= 57) && (exChars == null || exChars.indexOf((char) t) < 0)) {
     72                     code.append((char) t);
     73                     i++;
     74                 }
     75             }
     76             break;
     77         case 5: // 仅大写字母
     78             while (i < length) {
     79                 int t = r.nextInt(91);
     80                 if (t >= 65 && (exChars == null || exChars.indexOf((char) t) < 0)) {
     81                     code.append((char) t);
     82                     i++;
     83                 }
     84             }
     85             break;
     86         case 6: // 仅小写字母
     87             while (i < length) {
     88                 int t = r.nextInt(123);
     89                 if (t >= 97 && (exChars == null || exChars.indexOf((char) t) < 0)) {
     90                     code.append((char) t);
     91                     i++;
     92                 }
     93             }
     94             break;
     95         }
     96         return code.toString();
     97     }
     98 
     99     public static void main(String[] args) {
    100         // 10以内的随机数: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9    不包括10
    101         Random random = new Random();
    102         System.out.println(random.nextInt(10));
    103         
    104         // 大写字母
    105         for(int i = 65; i <= 90; i++) {
    106             System.out.print((char)i);
    107             System.out.print(" ");
    108         }
    109         System.out.println();
    110 
    111         // 小写字母
    112         for(int i = 97; i <= 122; i++) {
    113             System.out.print((char)i);
    114             System.out.print(" ");
    115         }
    116         System.out.println();
    117         
    118         // 数字
    119         for(int i = 48; i <= 57; i++) {
    120             System.out.print((char)i);
    121             System.out.print(" ");
    122         }
    123         System.out.println();
    124 
    125         System.out.println(VerifyCode.generateTextCode(0, 32, "0")); // 数字
    126         System.out.println(VerifyCode.generateTextCode(1, 32, "l")); // 大写字母和小写字母组合
    127         System.out.println(VerifyCode.generateTextCode(2, 32, "l")); // 数字大写字母、小写字母组合
    128         System.out.println(VerifyCode.generateTextCode(3, 32, "0")); // 数字和大写字母组合
    129         System.out.println(VerifyCode.generateTextCode(4, 32, "0")); // 数字和小写字母组合
    130         System.out.println(VerifyCode.generateTextCode(5, 32, "Z")); // 大写字母
    131         System.out.println(VerifyCode.generateTextCode(6, 32, "o")); // 小写字母
    132         
    133     }
    134 
    135 }

    输出:

     1 5
     2 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 
     3 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 
     4 0 1 2 3 4 5 6 7 8 9 
     5 62651637842124942893714462981794
     6 YMFcVREgxazamXFoAgkjKJimFohafisJ
     7 sZecd9cNP9hufE3ZrQIVxEKtR6tkUkVj
     8 3MJUZ6JD1QZSC4RNR6C4VC6YT4DWHHBY
     9 pdqdwia9csl8e89xneh1lvvnrh2btkdz
    10 HMTFHCGUXVABGIVSLRCWAGQDABYKQVDB
    11 uwikqkqaeiemvphctmyhxctdsfwrkkgh
  • 相关阅读:
    Swing 2
    Swing 1
    集合
    关于sql 模糊字段查询语句
    关于前端开发的几篇文章
    黄金点游戏
    word count
    四则运算
    软件工程——《构建之法》读后困惑
    自我介绍
  • 原文地址:https://www.cnblogs.com/xiejh/p/5155749.html
Copyright © 2011-2022 走看看