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. pyppeteer爬虫例子

    如果在centos上使用,需要安装下面的依赖 yum install pango.x86_64 libXcomposite.x86_64 libXcursor.x86_64 libXdamage.x8 ...

  2. WebSphere的jython编码的一个坑

    was5.1版本,用"name=" in line这类判断字符串包含的方式时,系统会提示报错 TypeError: string member test needs char le ...

  3. C++:vector中的v.at(0)和v[0]的区别

    设v是一个vector的对象, 如果v是非空的,则v.at(0)和v[0]是没有区别的,都是取数组中第一个值: 如果v是空的,则v.at(0)会抛出异常(exception std::out_of_r ...

  4. centos6.5环境基于corosync+cman+rgmanager实现RHCS及iscsi+gfs2+clvm的文件系统集群

    centos6.5环境基于corosync+cman+rgmanager实现RHCS及iscsi+gfs2+clvm文件系统集群 一.环境准备 服务器列表: ansible server : 192. ...

  5. Expm 8_1 区间划分问题

      [问题描述] 给定一组报告,其中的每个报告设置了一个开始时间si和结束时间fi.设计与实现一个算法,对这组报告分配最少数量的教室,使得这些报告能无冲突的举行. package org.xiu68. ...

  6. Python-html css 盒模型

    <!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>ht ...

  7. 性能测试十六:liunx下jmete配置环境变量

    修改环境变量后就不用每次手动输入路径,省时省事,减少命令长度和出错率 按Ctrl+L可进行翻页,翻页到最后一行,此处有java的环境变量 添加jmeter的目录和bin目录 此时,虽修改成功,但是并未 ...

  8. POJ 1328 Radar Installation【贪心】

    POJ 1328 题意: 将一条海岸线看成X轴,X轴上面是大海,海上有若干岛屿,给出雷达的覆盖半径和岛屿的位置,要求在海岸线上建雷达,在雷达能够覆盖全部岛屿情况下,求雷达的最少使用量. 分析: 贪心法 ...

  9. 【Java】 剑指offer(29) 顺时针打印矩阵

    本文参考自<剑指offer>一书,代码采用Java语言. 更多:<剑指Offer>Java实现合集   题目 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. 思 ...

  10. java中如何不自己写排序方法完成排序

    1.如果要排序的东西是放在一个数组里面的 1.1如果要排序的东西是基本数据类型,如int int[] c=new int[]{4,5,1,2,3,6,9}; Arrays.sort(c); Array ...