1.编写生成验证码的工具类:

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;

public class VerifyCode
{
private int w = 70;
private int h = 35;
private Random r = new Random();

private String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" };
private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";
private Color bgColor = new Color(255, 255, 255);
private String text;

private Color randomColor()
{
int red = this.r.nextInt(150);
int green = this.r.nextInt(150);
int blue = this.r.nextInt(150);
return new Color(red, green, blue);
}

private Font randomFont() {
int index = this.r.nextInt(this.fontNames.length);
String fontName = this.fontNames[index];
int style = this.r.nextInt(4);
int size = this.r.nextInt(5) + 24;
return new Font(fontName, style, size);
}

private void drawLine(BufferedImage image) {
int num = 3;
Graphics2D g2 = (Graphics2D)image.getGraphics();
for (int i = 0; i < num; ++i) {
int x1 = this.r.nextInt(this.w);
int y1 = this.r.nextInt(this.h);
int x2 = this.r.nextInt(this.w);
int y2 = this.r.nextInt(this.h);
g2.setStroke(new BasicStroke(1.5F));
g2.setColor(Color.BLUE);
g2.drawLine(x1, y1, x2, y2);
}
}

private char randomChar() {
int index = this.r.nextInt(this.codes.length());
return this.codes.charAt(index);
}

private BufferedImage createImage() {
BufferedImage image = new BufferedImage(this.w, this.h, 1);
Graphics2D g2 = (Graphics2D)image.getGraphics();
g2.setColor(this.bgColor);
g2.fillRect(0, 0, this.w, this.h);
return image;
}

public BufferedImage getImage() {
BufferedImage image = createImage();
Graphics2D g2 = (Graphics2D)image.getGraphics();
StringBuilder sb = new StringBuilder();

for (int i = 0; i < 4; ++i) {
String s = randomChar();
sb.append(s);
float x = i * 1.0F * this.w / 4.0F;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, this.h - 5);
}
this.text = sb.toString();
drawLine(image);
return image;
}

public String getText() {
return this.text;
}

public static void output(BufferedImage image, OutputStream out) throws IOException
{
ImageIO.write(image, "JPEG", out);
}
}

2.调用 验证码的工具类,创建Servlet保存验证码到Session中:

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class VerifyCodeServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
VerifyCode vc = new VerifyCode();
BufferedImage image = vc.getImage();

VerifyCode.output(image, response.getOutputStream());

request.getSession().setAttribute("vCode", vc.getText());
}
}

3.jsp中需要产生验证码:

<img src="/VerifyCodeServlet " id="yzm" />

<a href="" onclick="javascript:hyz()"></a>

function hyz(){

$("yzm").src="/VerifyCodeServlet ? a = " +new Date.getTime();    //加上时间戳解决IE浏览器的缓存

}

java生成一次性验证码的更多相关文章

  1. Java生成随机验证码

    package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  2. java生成简单验证码图片

    概要 最近项目需要用java实现输出随机验证码图片到前台,正好有机会接触下java的绘图类,完成需求后也有时间做个总结,写篇随笔记录下也希望能帮助到有同样需求的人! 需求流程图 1.生成随机数 在ja ...

  3. 如何用java生成随机验证码

     1.VerifyCode 类:   1 package com.HRuinger.enity;                          ImageIO.write(image, " ...

  4. java生成图形验证码

    效果图 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buf ...

  5. java生成汉字验证码

    java实现的汉字输入验证码,主要包含两个类,一个是生成验证码,一个是判断验证码输入是否正确,实现原理非常简单,将汉字和干扰线生成图片并将汉字保存到session,前台获取每次生成验证码图片并用文本框 ...

  6. java生成动态验证码

    import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;impor ...

  7. java生成随机验证码图片

    import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; i ...

  8. java生成前端验证码+验证「kaptcha」

    1.前言 kaptcha是一个非常实用的短信验证码生成工具,通过简单配置即可实现多样化的验证码. 2.引入依赖 <!--第三方验证码--><dependency>    < ...

  9. Java生成验证码原理(jsp)

     验证码的作用: 验证码是Completely Automated Public Turing test to tell Computers and Humans Apart(全自动区分计算机和人类的 ...

随机推荐

  1. AF_INET域与AF_UNIX域socket通信原理对比【转】

    转自:https://www.cnblogs.com/lfxiao/p/9672797.html 1.  AF_INET域socket通信过程 典型的TCP/IP四层模型的通信过程. 发送方.接收方依 ...

  2. XAF 与 CIIP

    XAF 与 CIIP:网站:http://www.uims.top, XAF技术博客:http://www.cnblogs.com/foreachlife/ tylike 升级到  DevExpres ...

  3. oracle instantclient_11_2 配置文件tnsnames.ora

    文件所在位置(不同版本位置可能不同): oracle\product\10.2.0\client_1\NETWORK\ADMIN\tnsnames.ora WDDB = (DESCRIPTION = ...

  4. Expm 1_2 实现快速排序的算法,并尝试采用不同的方法实现线性的划分过程.

    package org.xiu68.exp.exp1; public class Exp1_2 { //实现快速排序算法,采用不同的方法实现线性划分的过程 public static void mai ...

  5. 如何查看centos系统cpu/内存使用情况

    1.查看硬盘 [mushme@investide ~]$ df -ah 文件系统              容量  已用 可用 已用% 挂载点 /dev/cciss/c0d0p1     123G   ...

  6. 转:vue+canvas如何实现b站萌系登录界面

    https://juejin.im/post/5ae802a46fb9a07ac55fec04

  7. Node.js随手笔记

    参考文章:https://blog.csdn.net/IronKee/article/details/83780857 安装nvm的方法: 如果已经单独安装了node,建议卸载,然后继续向下看. 直接 ...

  8. 配置本地无密码 SSH登录远程服务器

    下面这幅图简单来说就是你本地有一把钥匙,服务器也有一把钥匙,当登录的时候本地的钥匙与服务器的进行对比,通过算法的判定,监测是否具有权限的用户 第一步,在本地配置这把钥匙生成私钥与公钥: 打开.ssh目 ...

  9. Webpack devServer中的 proxy 实现跨域

    Webpack dev server使用http-proxy解决跨域问题 文档资料 webpack关于webpack-dev-server开启proxy的官方介绍Vue-cli proxyTable ...

  10. html----常见的标签

    HTML标签: <strike>为文字加上一条中线</strike> <em>: 文字变成斜体.</em> 2<sup>2</sup& ...