JAVA图形验证码

生成验证码的jsp:

 1 <%@ page contentType="image/jpeg" language="java" import="java.util.*,java.awt.*,java.awt.image.*,javax.imageio.*" pageEncoding="utf-8"%>
 2 
 3 <%!
 4   Color getRandColor(int fc,int bc){
 5     Random random = new Random();
 6     if(fc > 255){
 7       fc = 255;
 8     }
 9     if(bc < 255){
10       bc = 255;
11     }
12     int r = fc +random.nextInt(bc-fc);
13     int g = fc +random.nextInt(bc-fc);
14     int b = fc +random.nextInt(bc-fc);
15 
16 
17     return new Color(r,g,b);
18   }
19 %>
20 
21 <%
22   //设置页面不缓存
23   response.setHeader("Pragma","no-cache");
24   response.setHeader("Cache-Control","no-catch");
25   response.setDateHeader("Expires",0);
26 
27   //在内存中创建图象
28   int width = 60;
29   int height = 42;
30   BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
31 
32   //创建图象
33   Graphics g = image.getGraphics();
34   //生成随机对象
35   Random random = new Random();
36   //设置背景色
37   g.setColor(getRandColor(200,250));
38   g.fillRect(0,0,width,height);
39   //设置字体
40   g.setFont(new Font("Tines Nev Roman",Font.PLAIN,24));
41   //随机产生干扰线
42
43   g.setColor(getRandColor(160,200));
44   for(int i = 0; i < 255; i++){
45     int x = random.nextInt(width);
46     int y = random.nextInt(height);
47     int xl = random.nextInt(12);
48     int yl = random.nextInt(12);
49     g.drawLine(x, y, x+xl, y+yl);
50   }
51   g.setColor(getRandColor(65,105));
52   for(int i = 0; i < 4; i++){
53     int x = random.nextInt(width);
54     int y = random.nextInt(height);
55     int xl = random.nextInt(12);
56     int yl = random.nextInt(12);
57     g.drawLine(x, y, x+xl, y+yl);
58   }
59 
60   //随机产生认证码,4位数字
61   String sRand = "";
62   for(int i = 0; i < 4; i++){
63     String rand = String.valueOf(random.nextInt(10));
64     sRand  += rand;
65     //将认证码显示到图象中
66     g.setColor(new Color(20 + random.nextInt(110),20 + random.nextInt(110),20 + random.nextInt(110)));
67     g.drawString(rand,13*i+6,25+random.nextInt(10));
68   }
69   session.setAttribute("rCode",sRand);
70   //图像生效
71   g.dispose();
72   //输出图像到页面
73   ImageIO.write(image,"JPEG",response.getOutputStream());
74   out.clear();
75   out = pageContext.pushBody();
76 %>

前台引用:

<img id="ckNumber" src="loginAction!getNumber.iol" />
<input type="text" name="code" class="ta_login_inputneed_notice" />

点击图片刷新验证码:

原理:使用jquery修改img的src属性,注意:src要和之前不同

方法:在链接后面随机生成一个参数使得链接和之前不一样,我这里是通过时间来生成的。

                <script>
                    $(document).ready(function () {
                        $("#ckNumber").click(function () {
                            var append = '?' + new Date().getTime() + 'a' + Math.random();
                            $(this).attr('src',$(this).attr('src') + append);
                        })

                    })

                </script>

后台验证(仅供参考):

if(!request.getParameter("code").equals(this.getSession().get("rCode"))) 

 

转载于:https://www.cnblogs.com/windspirit/p/5546998.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值