RSS订阅悠然探索,悠然索取
你的位置:首页 » JAVA » 正文

图片验证码

选择字号: 超大 标准 发布时间:2013-3-29 14:51:30 | 作者:admin | 0个评论 | 人浏览

 package net.lisoft.service.servlet;

 
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 CInentityServlet extends HttpServlet {
// 高度
private int width = 90;
// 宽度
private int height = 20;
 
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {
BufferedImage buffImg = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
Random random = new Random();
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 设置字体
Font font = new Font("Times New Roman", Font.BOLD, 20);
g.setFont(font);
// 画边框
g.setColor(Color.GRAY);
g.drawRect(0, 0, width - 1, height - 1);
g.setColor(Color.GRAY);
// 随机产生干扰线
for (int i = 0; i < 50; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int x1 = random.nextInt(2);
int y1 = random.nextInt(2);
g.drawLine(x, y, x + x1, y + y1);
}
// 数字字母集合(为方便辨认只有小写字母)
char[] numbersAndLettersStore = ("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
.toCharArray();
String randomCode = "";// 这个地方不能为null,如果为null那么session中也会出现null,不知道什么原因
int red = 0, green = 0, blue = 0;
for (int i = 0; i < 4; i++) {
red = random.nextInt(255);
green = random.nextInt(255);
blue = random.nextInt(255);
g.setColor(new Color(red, green, blue));
String tem = String.valueOf(numbersAndLettersStore[random
.nextInt(numbersAndLettersStore.length)]);
randomCode += tem;
g.drawString(tem, 15 * i + 16, 17);
}
// 验证码保存到Session范围
HttpSession session = req.getSession();
session.setAttribute("logincode", randomCode.toString());
// 禁止缓存
resp.setHeader("Prama", "no-cache");
resp.setHeader("Coche-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
// 将图像输出到
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
}
 

标签:

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。