java生成一次性验证码
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生成一次性验证码的更多相关文章
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- java生成简单验证码图片
概要 最近项目需要用java实现输出随机验证码图片到前台,正好有机会接触下java的绘图类,完成需求后也有时间做个总结,写篇随笔记录下也希望能帮助到有同样需求的人! 需求流程图 1.生成随机数 在ja ...
- 如何用java生成随机验证码
1.VerifyCode 类: 1 package com.HRuinger.enity; ImageIO.write(image, " ...
- java生成图形验证码
效果图 import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.Buf ...
- java生成汉字验证码
java实现的汉字输入验证码,主要包含两个类,一个是生成验证码,一个是判断验证码输入是否正确,实现原理非常简单,将汉字和干扰线生成图片并将汉字保存到session,前台获取每次生成验证码图片并用文本框 ...
- java生成动态验证码
import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.Graphics2D;impor ...
- java生成随机验证码图片
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; i ...
- java生成前端验证码+验证「kaptcha」
1.前言 kaptcha是一个非常实用的短信验证码生成工具,通过简单配置即可实现多样化的验证码. 2.引入依赖 <!--第三方验证码--><dependency> < ...
- Java生成验证码原理(jsp)
验证码的作用: 验证码是Completely Automated Public Turing test to tell Computers and Humans Apart(全自动区分计算机和人类的 ...
随机推荐
- Python3学习笔记16-错误和异常
使用try...except可以处理异常 异常处理 import sys try: print('try...') r = 10/0 print('result:',r) except ZeroDiv ...
- 转载:Java中的字符串常量池详细介绍
引用自:http://blog.csdn.net/langhong8/article/details/50938041 这篇文章主要介绍了Java中的字符串常量池详细介绍,JVM为了减少字符串对象的重 ...
- unicode-range特定字符使用font-face自定义字体
链接: https://www.zhangxinxu.com/wordpress/2016/11/css-unicode-range-character-font-face/
- Clipboard深度实践与采坑记录
1.css禁止选择导致IOS无法复制 body{ -webkit-touch-callout: none; -webkit-user-select: none; -khtml-user-select: ...
- java 对象锁学习
机制 锁机制是用来解决多线程共享资源时产生的冲突问题的.java 为每一个对象关联一个对象锁,通常把锁分为对象锁和类锁,他们的本质都是对象锁,只不过对象锁关联的是类的 Object 对象 (java. ...
- js继承的几种实现方法
一.用function实现: function Person(name) { this.name = name; } Person.prototype.getName = function() { r ...
- 测试开发之前端——No8.HTML5中的媒介事件
媒介事件 由视频.图像以及音频等媒介触发的事件. 适用于所有 HTML 5 元素,不过在媒介元素(诸如 audio.embed.img.object 以及 video)中最常用: 属性 值 描述 on ...
- PHP获取数组最后一个元素的键和值
<?php /** * PHP获取数组中最后一个元素下标和值 */ $arr = ['1' => 'name', '3' => 2, 5 => 6, 'name' => ...
- Java容器---字符容器StringBuffer & StringBuilder
1.字符串对象 (1)定义 ---String 字符串常量,是不可改变的量,也就是创建后就不能在修改了: --- StringBuffer 字符串变量(线程安全),StringBuffer对象的值都是 ...
- python接口自动化测试十:字典、字符串、json之间的简单处理
# 字典 t = { # 键:值 '': '', '': '' } # 字典 # dict key: value key是唯一的,无序的 h = { ...