Java 随机生成验证码,支撑大小写字母、数字、随机字体

    添加时间:2013-5-9 点击量:
    查看代码

    java随机产生验证码,可以随机生成数字、大写字母、小写字母。还可以随机生成文字字体、及大小。在上方可能字体都不不合、大小不等。 
    

    package com.hoo.util;


    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.util.Random;

    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    /
    <b>function:</b> 验证码生成对象类
    @project NetWorkService
    @package com.hoo.util
    @fileName ValidCodeUtils.java
    @createDate 2010-8-3 下午03:05:50
    @author hoojo
    /
    @SuppressWarnings(
    unused
    public class ValidCodeUtils {
    /
    验证码宽度
    /
    public static int WIDTH = 60;
    /
    验证码高度
    /
    public static int HEIGHT = 20;

    /
    验证码靠山色彩COLOR_FC_BG 该当小于COLOR_BC_BG
    /
    public static int COLOR_FC_BG = 200;
    /
    验证码靠山色彩COLOR_FC_BG 该当小于COLOR_BC_BG
    /
    public static int COLOR_BC_BG = 250;

    /
    验证码靠山干扰线色彩COLOR_FC_LINE 该当小于COLOR_BC_LINE
    /
    public static int COLOR_FC_LINE = 160;
    /
    验证码靠山干扰线色彩COLOR_FC_LINE 该当小于COLOR_BC_LINE
    /
    public static int COLOR_BC_LINE = 200;

    /
    验证码色彩COLOR_FC_CODE 该当小于COLOR_BC_CODE
    /
    public static int COLOR_FC_CODE = 20;
    /
    验证码色彩COLOR_FC_CODE 该当小于COLOR_BC_CODE
    /
    public static int COLOR_BC_CODE = 170;

    /
    生成在指定局限内的色彩
    @param fc 局限fc color值 小于255
    @param bc 局限bc color值 小于255
    @return Color
    /
    private static Color getRandColor(int fc, int bc) {
    Random random
    = new Random();
    if (fc < 0
    fc
    = 0;
    if (bc < 0
    bc
    = 1;
    if (fc > 255
    fc
    = 255;
    if (bc > 255
    bc
    = 255;
    if (bc == fc)
    bc
    += 10;
    int temp = 0;
    if (bc < fc) {
    temp
    = bc;
    bc
    = fc;
    fc
    = temp;
    }
    int r = fc + random.nextInt(bc - fc);
    int g = fc + random.nextInt(bc - fc);
    int b = fc + random.nextInt(bc - fc);
    return new Color(r, g, b);
    }

    /
    <b>function:</b> 生成办法
    @createDate 2010-8-3 下午03:06:22
    @author hoojo
    @param request HttpServletRequest
    @param response HttpServletResponse
    @return boolean
    @throws Exception
    /
    public static boolean getImage(HttpServletRequest request, HttpServletResponse response) throws Exception{
    response.reset();
    response.setContentType(
    image/jpeg);
    // 设置页面不缓存
    response.setHeader(Pragma, No-cache);
    response.setHeader(
    Cache-Control, no-cache);
    response.setDateHeader(
    Expires, 0);
    // 在内存中创建图象
    BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);

    // 获取图形高低文
    Graphics img = image.getGraphics();
    // 生成随机类
    Random random = new Random();

    // 设定靠山色
    img.setColor(getRandColor(COLOR_FC_BG, COLOR_BC_BG));
    img.fillRect(
    0, 0, WIDTH, HEIGHT);

    // 设定字体
    img.setFont(new Font(Times New Roman, Font.PLAIN, 18));

    // 画边框
    // g.setColor(new Color());
    // g.drawRect(0,0,width-1,height-1);

    // 随机产生155条干扰线,使图象中的认证码不易被其它法度探测到
    img.setColor(getRandColor(COLOR_FC_LINE, COLOR_BC_LINE));
    forint i = 0; i < 155; i++) {
    int x = random.nextInt(WIDTH);
    int y = random.nextInt(HEIGHT);
    int xl = random.nextInt(12);
    int yl = random.nextInt(12);
    img.drawLine(x, y, x
    + xl, y + yl);
    }

    // 取随机产生的认证码(4位数字)
    String codeValue = ;
    forint i = 0; i < 4; i++) {
    //String rand = String.valueOf(random.nextInt(10));
    String rand = getRandomChar();
    codeValue
    = codeValue.concat(rand);
    img.setFont(getRandomFont());
    //随机字体
    // 将认证码显示到图象中
    img.setColor(getRandColor(COLOR_FC_CODE, COLOR_BC_CODE));
    img.drawString(rand,
    13 i + 6, 16);
    }
    request.getSession().setAttribute(
    codeValue, codeValue);
    // 图象生效
    img.dispose();
    // 输出图象到页面
    return ImageIO.write(image, JPEG, response.getOutputStream());
    }

    /
    随机生成字符,含大写、小写、数字
    <b>function:</b> 功能
    @createDate 2010-8-23 上午10:33:55
    @author hoojo
    @return
    /
    public static String getRandomChar() {
    int index = (int) Math.round(Math.random() 2);
    String randChar
    = ;
    switch (index) {
    case 0://大写字符
    randChar = String.valueOf((char)Math.round(Math.random() 25 + 65));
    break;
    case 1://小写字符
    randChar = String.valueOf((char)Math.round(Math.random() 25 + 97));
    break;
    default://数字
    randChar = String.valueOf(Math.round(Math.random() 9));
    break;
    }
    return randChar;
    }

    /
    <b>function:</b> 随机生成字体、文字大小
    @createDate 2010-8-23 上午10:44:22
    @author hoojo
    @return
    /
    public static Font getRandomFont() {
    String[] fonts
    = {Georgia, Verdana, Arial, Tahoma, Time News Roman, Courier New, Arial Black, Quantzite};
    int fontIndex = (int)Math.round(Math.random() (fonts.length - 1));
    int fontSize = (int) Math.round(Math.random() 4 + 16);
    return new Font(fonts[fontIndex], Font.PLAIN, fontSize);
    }
    }

    此中验证码的值是保存在session中:request.getSession().setAttribute(
    codeValue, codeValue);
    斗劲用户输入的值和session中的codeValue是否相等即可;

    下面是jsp页面调用servlet:ValidCodeServlet.java

    ValidCodeServlet中调用了上方的ValidCodeUtils 验证码生成对象类

    package com.hoo.servlet;

    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    import com.hoo.util.ValidCodeUtils;

    @SuppressWarnings(
    serial
    public class ValidCodeServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    try {
    ValidCodeUtils.getImage(request, response);
    }
    catch (Exception e) {
    e.printStackTrace();
    }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doGet(request, response);
    }
    }



    jsp页面调用servlet办法即可



    js:reloadValidCode办法

    function reloadValidCode(o) {
    o.src
    = ¥{pageContext.request.contextPath }/validCodeServlet?timed= + new Date().getMilliseconds();
    }
    这里的
    timed= + new Date().getMilliseconds();是须要防止IE缓存用的

    html标签:
    <img src=¥{pageContext.request.contextPath }/validCodeServlet title=看不清,点击刷新 onclick=reloadValidCode(this)/>
    直接跟Servlet名称设备的url即可,和web.xml设备对应。首要调用路径¥{pageContext.request.contextPath }
    /validCodeServlet如许会带上根目次,斗劲保险。

    web.xml中validCodeServlet设备
    <servlet>
    <servlet-name>validCodeServlet</servlet-name>
    <servlet-class>com.hoo.servlet.ValidCodeServlet</servlet-class>
    </servlet>

    <servlet-mapping>
    <servlet-name>validCodeServlet</servlet-name>
    <url-pattern>/validCodeServlet</url-pattern>
    </servlet-mapping>




    查看代码

      1 package cn.edu.bzu;
    
    2
    3 import java.awt.Color;
    4 import java.awt.Font;
    5 import java.awt.Graphics2D;
    6 import java.awt.image.BufferedImage;
    7 import java.io.IOException;
    8 import java.util.Random;
    9
    10 import javax.imageio.ImageIO;
    11 import javax.servlet.ServletException;
    12 import javax.servlet.ServletOutputStream;
    13 import javax.servlet.http.HttpServlet;
    14 import javax.servlet.http.HttpServletRequest;
    15 import javax.servlet.http.HttpServletResponse;
    16
    17 import com.sun.image.codec.jpeg.JPEGCodec;
    18 import com.sun.image.codec.jpeg.JPEGImageEncoder;
    19
    20 public class GetKey extends HttpServlet {
    21 //定义可选择的字符
    22 public static final char[] CHARS={2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,U,V,W,X,Y,Z};
    23 static Random random=new Random();
    24
    25 public String getRandomString(){
    26
    27 StringBuffer buffer=new StringBuffer();
    28 forint i=0;i<6;i++){ //生成六个字符
    29 buffer.append(CHARS[random.nextInt(CHARS.length)]);
    30 }
    31 return buffer.toString();
    32 }
    33
    34 public static Color getRandomColor(){ //获得随机色彩
    35 return new Color(random.nextInt(255),random.nextInt(255),random.nextInt(255));
    36 }
    37
    38 public static Color getReverseColor(Color c){ //获得色彩的反色
    39 return new Color(255-c.getRed(),255-c.getGreen(),255-c.getBlue());
    40 }
    41
    42 public void doGet (HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
    43 response.setContentType(image/jpeg); //设置输出类型
    44 String randomString=getRandomString();
    45 //将getSession()设置为true,当会话不存在是返回null
    46 request.getSession(true).setAttribute(randomString,randomString);
    47
    48 //设置的宽、高
    49 int width=100;
    50 int height=30;
    51
    52 Color bcolor=getRandomColor(); //前风景
    53 Color fcolor=getReverseColor(getRandomColor()); //设置靠山色
    54
    55 //创建一个彩色
    56 BufferedImage bimage=new BufferedImage(width,height,BufferedImage.TYPE_INT_BGR);
    57 //创建画图对象
    58 Graphics2D g=bimage.createGraphics();
    59 //字体样式为宋体,加粗,20磅
    60 g.setFont(new Font(宋体,Font.BOLD,20));
    61 //先画出靠山色
    62 g.setColor(bcolor);
    63 g.fillRect(0,0,width,height);
    64 //再画出前风景
    65 g.setColor(fcolor);
    66 //绘制随机字符
    67 g.drawString(randomString, 20,22);
    68 //画出干扰点
    69 forint i=0,n=random.nextInt(100);i<n;i++){
    70 g.drawRect(random.nextInt(width), random.nextInt(height),1, 1);
    71 }
    72
    73 ServletOutputStream outstream=response.getOutputStream();
    74 JPEGImageEncoder encoder=JPEGCodec.createJPEGEncoder(outstream);
    75
    76 encoder.encode(bimage);
    77 outstream.flush();
    78
    79
    80 }
    81 }
    82
    83 ====================================================
    84 前台的JSP文件:
    85 <%@ page language=java contentType=text/html;charset=utf-8 %>
    86 <%@ page import=cn.edu.bzu.GetKey %>
    87 <jsp:useBean id=show scope=page class=cn.edu.bzu.GetKey/>
    88 <html>
    89 <head><title>登岸</title>
    90 <script language=javascript type=text/javascript>
    91 function changeImage(){
    92 document.getElementById(ch).disabled=true;
    93 document.getElementById(imc).src=http://localhost:8080/ExerciseTest/GetKey?ts=+newDate().getTime();
    94 }
    95 </script>
    96 </head>
    97 <body>
    98 <form action=GoTest method=get>
    99 <table width=400 height=200 border=1 borderColor=blue>
    100 <tr>
    101 <td colspan=4 align=center>
    102 后台登岸
    103 </td>
    104 </tr>
    105 <tr>
    106 <td>用户名:</td>
    107 <td colspan=3><input type=text name=username class=width:200px;height:30px;font-family:微软雅黑/></td>
    108 </tr>
    109 <tr>
    110 <td>
    111 密&nbsp;&nbsp;码:
    112 </td>
    113 <td colspan=3>
    114 <input type=password name=pass class=width:200px;height:30px;/>
    115 </td>
    116 </tr>
    117 <tr>
    118 <td>
    119 验证码:
    120 </td>
    121 <td>
    122 <input type=text name=checkkey class=width:80px;height:30px;padding-top:4px;/>
    123 </td>
    124 <td>
    125 <img src=http://localhost:8080/ExerciseTest/GetKey id=imc onload=ch.disabled=false;/>
    126 </td>
    127 <td>
    128 <a href=javascript:onclick=changeImage() id=ch>看不清,换一张</a>
    129 </td>
    130 </tr>
    131 <tr>
    132 <td align=center >




    原来,再大的房子,再大的床,没有相爱的人陪伴,都只是冰冷的物质。而如果身边有爱人陪伴,即使房子小,床小,也觉得无关紧要,因为这些物质上面有了爱的温度,成了家的元素。—— 何珞《婚房》#书摘#
    分享到: