Java实现随机验证码具体代码
本文实例为大家分享了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;
}
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
以上是 Java实现随机验证码具体代码 的全部内容, 来源链接: utcz.com/p/210896.html