当前位置: 移动技术网 > IT编程>开发语言>Java > Java实现随机验证码具体代码

Java实现随机验证码具体代码

2019年07月22日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java随机生成验证码的具体代码,供大家参考,具体内容如下

import java.awt.color;

import java.awt.font;
import java.awt.graphics2d;
import java.awt.image.bufferedimage;
import java.io.ioexception;
import java.util.random;

import javax.imageio.imageio;
import javax.servlet.servletexception;
import javax.servlet.servletoutputstream;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;


public class randimage extends httpservlet {

/**
* constructor of the object.
*/
  public randimage() {
    super();
  }

    private int imgwidth = 0; //图片宽度

    private int imgheight = 0; //图片的高度

    private int codecount = 0; //图片里面字符的个数

    private int x = 0;

    private int fontheight; //字体的高度
  
    private int codey; 

    private string fontstyle; //字体样式


    //序列化id 避免重复
    private static final long serialversionuid = 128554012633034503l;

    /**
    * 初始化配置参数
    */

public void init() throws servletexception {

  // 宽度
  string strwidth = "200";
  // 高度
  string strheight ="80";
  // 字符个数
  string strcodecount ="5";
  //字体
  fontstyle = "times new roman";

// 将配置的信息转换成数值
  try {
    if (strwidth != null && strwidth.length() != 0) {
    imgwidth = integer.parseint(strwidth);
    }
  if (strheight != null && strheight.length() != 0) {
  imgheight = integer.parseint(strheight);
    }
  if (strcodecount != null && strcodecount.length() != 0) {
  codecount = integer.parseint(strcodecount);
    }
  } catch (numberformatexception e) {
    e.printstacktrace();
  }
  
    x = imgwidth / (codecount + 1); //字符间距
    fontheight = imgheight - 2; //字体的高度
    codey = imgheight - 12; // 代码高度
  }

    protected void processrequest(httpservletrequest request,
    httpservletresponse response) throws servletexception, ioexception {

    //输出流设置
    response.setcontenttype("image/jpeg"); //输出格式
    response.setheader("pragma", "no-cache");//不缓存 重新生成
    response.setheader("cache-control", "no-cache");//不缓存 重新生成
    response.setdateheader("expires", 0); //0秒失效 也是不缓存
    httpsession session = request.getsession(); //获取session 会话

    // 在内存中创建图象
    bufferedimage image = new bufferedimage(imgwidth, imgheight, bufferedimage.type_int_rgb);
    // 获取图形上下文  
    graphics2d g = image.creategraphics();
    // 生成随机类
    random random = new random(); //随机类
    // 设定矩形的背景色
    g.setcolor(color.white);
    //填充矩形rect为白色
    g.fillrect(0, 0, imgwidth, imgheight);

    // 设定边框字体
    g.setfont(new font(fontstyle, font.plain + font.italic, fontheight));
    //设置边框的颜色
    g.setcolor(new color(55, 55, 12));
    // 画边框
    g.drawrect(0, 0, imgwidth - 1, imgheight - 1);

    // 随机产生160条干扰线,使图象中的认证码不易被其它程序探测到
    g.setcolor(getrandcolor(160, 200));
    for (int i = 0; i < 160; i++) {
    int x = random.nextint(imgwidth);
    int y = random.nextint(imgheight);
    int xl = random.nextint(12);
    int yl = random.nextint(12);
    g.drawline(x, y, x + xl, y + yl);
  }
  
    // 取随机产生的认证码(4位数字)
    string srand = "";
    int red = 0, green = 0, blue = 0;
    for (int i = 0; i < codecount; i++) { //循环生成codecount个随机字符

    //通过rgb三色随机得到新的颜色
    red = random.nextint(255);
    green = random.nextint(255);
    blue = random.nextint(255);
    //随机得到一个0 1 2 的数字
    int wordtype = random.nextint(3);//随机得到0-2之间的3个数字
    char retword = 0;
    //0 数字 1 小写字母 2 大写字母
    switch (wordtype) {
  case 0:
    retword = this.getsinglenumberchar(); //得到0-9的char型数字
  break;
  case 1:
    retword = this.getlowerorupperchar(0); //得到小写的char型字母
  break;
  case 2:
    retword = this.getlowerorupperchar(1); //得到大写的char型字母
  break;
  }
    srand += string.valueof(retword); //将得到的随机字符 连接起来
    g.setcolor(new color(red, green, blue)); //设置一个颜色
    g.drawstring(string.valueof(retword), 2+(i) * x, codey); //将字符写到图片中 对应的位置
  }
    // 将认证码存入session
    session.setattribute("rand", srand); //将得到的随机字符存入到session回话中,验证的时候可以调用
    // 图象生效
    g.dispose(); //释放g 对象
    servletoutputstream responseoutputstream = response.getoutputstream(); //输出流
    // 输出图象到页面
    imageio.write(image, "jpeg", responseoutputstream); //以jpeg的格式输出

    // 以下关闭输入流!
    responseoutputstream.flush();//刷新并关闭流
    responseoutputstream.close();
  }

    color getrandcolor(int fc, int bc) {// 给定范围获得随机颜色
    random random = new random();
    if (fc > 255)
    fc = 255;
    if (bc > 255)
    bc = 255;
    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);
  }

    protected void doget(httpservletrequest request,
    httpservletresponse response) throws servletexception, ioexception {
    processrequest(request, response);
  }

    protected void dopost(httpservletrequest request,
    httpservletresponse response) throws servletexception, ioexception {
    processrequest(request, response);
  }


    // 将整型随机数字转换成char返回
    private char getsinglenumberchar() {
    random random = new random();
    int numberresult = random.nextint(10);
    int ret = numberresult + 48; //将字符 '0‘ 转换成ascall码的时候 就是48
    return (char) ret;
  }

    //得到26个字符
    private char getlowerorupperchar(int upper) {
    random random = new random();
    int numberresult = random.nextint(26);
    int ret = 0;
    if (upper == 0) {// 小写
    ret = numberresult + 97;
  } else if (upper == 1) {// 大写
    ret = numberresult + 65;
    }
    return (char) ret;
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网