生成验证码
来源:互联网 发布:中信证券手机版软件 编辑:程序博客网 时间:2024/06/05 12:05
一个生成验证码的jsp片段:
<%@page language="java" import="java.sql.*" errorPage=""%><%@page contentType="image/jpeg"import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*"%><%!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);}%><%response.setHeader("Pragma","No-cache");response.setHeader("Cache-Control","No-cache");response.setDateHeader("Expires",0);int width=80,height=30;BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();//获取图形上下文Random random = new Random();//生成随机对象g.setColor(getRandColor(200,250));g.fillRect(0,0,width,height);g.setFont(new Font("Times New Roman",Font.PLAIN,18));//设定字体//随机产生155条干扰线,使图像中的验证码不易被其他程序探测到g.setColor(getRandColor(160,200));for(int i=0;i<155;i++){int x = random.nextInt(width);int y = random.nextInt(height);int x1 = random.nextInt(12);int y1 = random.nextInt(12);g.drawLine(x,y,x+x1,y+y1);}//取随机产生的验证码(4位数字)String sRand = "";for(int i=0;i<4;i++){String rand = String.valueOf(random.nextInt(10));sRand+=rand;//将验证码显示到图像中g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//设置显示的随机字符的颜色g.drawString(rand,13*i+6,16);//-----------------------------------------有一些疑问}session.setAttribute("rand",sRand);//将验证码存入SESSIONg.dispose();//图像生效//输出图像到页面ImageIO.write(image,"JPEG",response.getOutputStream());out.clear();out = pageContext.pushBody();%>
- 验证码生成及验证
- 生成验证码并验证
- 验证码生成与验证
- C#生成验证码
- C#生成验证码
- 生成验证码
- 验证码生成
- 生成验证码
- JSP生成验证码
- 生成验证码
- 生成验证码
- 数字验证码生成
- 动态生成验证码
- 生成验证码(1)
- 生成验证码(2)
- c# 生成验证码
- 生成验证码图像
- 生成验证码
- Html5 系列之:地理位置Geolocation
- 10G,11G恢复表的统计信息
- PHP插入法排序
- Html5 系列之:本地存储Storage
- Html5 系列之:桌面通知Notification
- 生成验证码
- 栈的定义和存储以及算法实现
- C/C++开发语言系列之16---C++不同类型数据间的转换
- NGUI v2.0 UI_2:UIAtlas UI图集类
- sync 命令
- LeetCode Gray Code
- jQuery ajax请求 - ajaxStart() ajaxSend() ajaxStop() ajaxComplete()方法
- UML 组件图
- c# 中委托于事件区别