验证码生成

1:前台
<li class="yanzm"><div class="yzm">
                <input name="authCode" id="authCode" style="color:black;width:30px;" type="text"/> <img src="${ctx }/front/authimg.do" id="authimg" width="70" height="25"/>
                <span id="authCode1" style="color:red;"></span>
                </div>
               </li>
2:script中添加
$(document).ready(function() {
$("#authimg").click(function(){
var timenow = new Date().getTime();
document.getElementById("authimg").src='${ctx }/front/authimg.do?d='+timenow;
});
});
3:后台java编码

public String authImage(HttpServletRequest request, 
HttpServletResponse response, ModelMap model) throws Exception { 
BufferedImage img = new BufferedImage(68, 22, 
BufferedImage.TYPE_INT_RGB); 
// 得到该图片的绘图对象 
Graphics g = img.getGraphics(); 
Random r = new Random(); 
Color c = new Color(200, 150, 255); 
g.setColor(c); 
// 填充整个图片的颜色 
g.fillRect(0, 0, 68, 22); 
// 向图片中输出数字和字母 
StringBuffer sb = new StringBuffer(); 
char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); 
int index, len = ch.length; 
for (int i = 0; i < 4; i++) { 
index = r.nextInt(len); 
g 
.setColor(new Color(r.nextInt(88), r.nextInt(188), r 
.nextInt(255))); 
g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 22));// 输出的字体和大小 
g.drawString("" + ch[index], (i * 15) + 3, 18);// 写什么数字,在图片的什么位置画 
sb.append(ch[index]); 
} 
request.getSession().setAttribute("piccode", sb.toString()); 
ImageIO.write(img, "JPG", response.getOutputStream()); 
return null; 
} 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值