生成验证码
来源:互联网 发布:mac excel快捷键大全 编辑:程序博客网 时间:2024/05/29 23:45
使用技术:jsp和Servlet
流程:
画图片
缓存
画笔
画图片,保存到流中
宽度 高度
OutputStream
画字符串
画字符串的方法
字符串保存到流中
Random
final StringSEED=”0-9 A-Z a-z”
画干扰线
画干扰点
代码:
util包中:
生成图片的类:
/**
* 此类方法只管画图片,其他一律不管
*/
public class GeneratorRandImage {
// 指定图片的宽度和高度
private int width = 120;
private int height = 30;
private OutputStreamout = null;// 不能进行实例化
// 定义随机生成器,产生随机数
private Randomrand = new Random();
/**
* 构造方法,接收指定的流
*/
public GeneratorRandImage(OutputStream out) {
this.out = out;
}
/**
* 随机数产生方法
* @return
*/
public String getRandString(int num) {
String str = "";
// 定义一个字符串的种子,随机字符串从该种子里面拿
final String SEED ="0123456789ABCDEFGHJKLMNOPQRSTUVWXYZabcdefghigklmnopqrstuvwxyz";
for (int i = 0; i < num; i ++){
char s = SEED.charAt(rand.nextInt(SEED.length()));// 索引
str += s;
}
return str;
}
/**
* 得到随机颜色
* @return
*/
public Color getRandColor() {
int red =rand.nextInt(256);
int green =rand.nextInt(256);
int blue =rand.nextInt(256);
return new Color(red, green, blue);
}
/**
* 获得随机图片
*/
public StringgetRandImage() {
// 设置缓存,用来保存图片
BufferedImage bufImg = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
// 获得画笔
Graphics g = bufImg.getGraphics();
// 设置背景为白色
g.setColor(Color.WHITE);
g.fillRect(0, 0, width, height);
// 设置边框
g.setColor(Color.BLACK);
g.drawRect(0, 0, width - 1,height - 1);
// 设置字体
g.setFont(new Font("楷体", Font.BOLD, 22));
// 画字符串
String strCode = getRandString(5);
for (int i = 0; i < strCode.length(); i ++) {
// 设置字体的颜色
g.setColor(getRandColor());
g.drawString(String.valueOf(strCode.charAt(i)),10 + 22 * i, 20);
}
// 画干扰线
for (int i = 0; i < 5; i++) {
// 设置干扰线的颜色
g.setColor(getRandColor());
// 设置直线的连点坐标
int x1 =rand.nextInt(width);
int y1 =rand.nextInt(height);
int x2 =rand.nextInt(width);
int y2 =rand.nextInt(height);
g.drawLine(x1, y1, x2, y2);
}
// 画干扰点
for (int i = 0; i < 20; i++) {
// 设置干扰点的颜色
g.setColor(getRandColor());
// 设置干扰点的x、y坐标
int x =rand.nextInt(width);
int y =rand.nextInt(height);
g.drawOval(x, y, 2, 2);
}
// 关闭画笔,显示效果
g.dispose();
// 把图片写入到流中
try {
ImageIO.write(bufImg, "jpeg", out);
} catch (IOException e) {
e.printStackTrace();
}
return strCode;
}
}
Action(Servlet一块):产生验证码图片的Action:
public class GetRandImageAction extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
// 获取流,指定流给图片
OutputStream out = response.getOutputStream();
// 实例化GeneratorRandImage
GeneratorRandImage fr = new GeneratorRandImage(out);
// 调用产生图片的方法,同时产生验证码
String strCode = fr.getRandImage();
// 将产生的验证码进行绑定
request.getSession().setAttribute("code", strCode);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
进行验证码验证的Action:
public class CheckCodeAction extends HttpServlet {
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
// 获取用户输入的验证码
String strCode = request.getParameter("code");
// 获取绘画对象。从Session里面获取正确的验证码
String strCode1 = (String) request.getSession().getAttribute("code");
System.out.println("strCode:" + strCode);
System.out.println("strCode1:" + strCode1);
PrintWriter out = response.getWriter();
if (strCode.equalsIgnoreCase(strCode1)) {
out.println("OK");
} else {
out.println("NO");
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
注意:别忘了在web.xml文件中进行配置。
页面中:
<body>
<formaction="doCheckCode"method="post">
请输入验证码:<inputtype="text"name="code"/>
<imgalt="看不清,点击切换图片"src="doJpg"onclick="history.go(0)">
<inputtype="submit"value="提交">
</form>
</body>
实现效果:- 验证码生成及验证
- 生成验证码并验证
- 验证码生成与验证
- C#生成验证码
- C#生成验证码
- 生成验证码
- 验证码生成
- 生成验证码
- JSP生成验证码
- 生成验证码
- 生成验证码
- 数字验证码生成
- 动态生成验证码
- 生成验证码(1)
- 生成验证码(2)
- c# 生成验证码
- 生成验证码图像
- 生成验证码
- Ubuntu12.10部署Spark0.9.0
- 让IE、Firefox、Chrome中的LI都支持display-block的兼容写法
- Box2d学习笔记一:在cocos2d-x中启动调试绘图
- 字符串
- 为什么寄存器速度大于内存
- 生成验证码
- Linux 的jobs 命令,fg 命令 ,&后台运行命令
- js中innerHTML与innerText的用法与区别
- Android RelativeLayout属性
- 记录文件操作及其相关字符串操作
- oracle四个重要的后台进程(DBWR / LGWR / ARCH / CKPT)
- Sysbase数据库性能优化
- 9-39
- spring定时器