java实现图片验证码-mile米乐体育

本文实例为大家分享了java实现图片验证码的具体代码,供大家参考,具体内容如下

目的:

1) 验证操作者是否是人

2) 防止表单重复提交

生成验证码的要点:

1) 使用java代码生成图片对象

buf恰卡编程网feredimage

2) 使用random生成随机字符串

3) 将图片对象用

imageio.write(图片对象, "jpeg|png", 响应字节输出流)

返回给客户端浏览器

验证验证码:

要点:

1) 将验证码文字存入session作用域

2) 表单提交时,对比session中的验证码和表单中的验证码

captchautils.java

package cn.seeker.utils; import javax.imageio.imageio; import java.awt.*; import java.awt.image.bufferedimage; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.outputstream; import java.util.*; import java.util.list; public class captchautil { public static string [] fuhao = {" ","-","*","/"}; public static int num = 0; //生成随机算式 public static string random(){ list list = new arraylist(); random ran = new random(); int n1 = ran.nextint(10); int n2 = ran.nextint(10); string f = fuhao[ran.nextint(fuhao.length)]; stringbuilder sb = new stringbuilder(4); switch (f){ case " ": num = n1 n2; break; case "-": num =n1-n2; break; case "*": num = n1*n2; break; case "/": if(n2 == 0){ n2 = ran.nextint(10) 1; num = n1/n2; }else{ num = n1/n2; } break; } sb.append(n1); sb.append(f); sb.append(n2); sb.append("="); return sb.tostring(); } public static void outputimage(string str, outputstream os){ random ran = new random(); //创建图片对象 bufferedimage img =new bufferedimage(100,40, bufferedimage.type_int_rgb); //获取画布 graphics g = img.getgraphics(); //设置背景 g.setcolor(color.white); g.fillrect(0,0,100,40); //写字 g.setcolor(color.black); g.setfont(new font("微软雅黑",font.plain,30)); g.drawstring(str,10,40); //随机三条线 g.drawline(ran.nextint(100),ran.nextint(40), ran.nextint(100),ran.nextint(40)); g.drawline(ran.nextint(100),ran.nextint(40), ran.nextint(100),ran.nextint(40)); g.drawline(ran.nextint(100),ran.nextint(40), ran.nextint(100),ran.nextint(40)); // 4. 输出结果 // 常见的压缩格式:jpeg(有损压缩), png(无损压缩) ... try { imageio.write(img, "png", os); } catch (ioexception e) { e.printstacktrace(); } } }

regist.jsp

<%-- created by intellij idea. user: administrator date: 2018/10/14 time: 15:43 to change this template use file | settings | file templates. --%> <%@ page contenttype="text/html;charset=utf-8" language="java" %> 注册

输入验证码:

captchaservlet.java

package cn.seeker.controller; import cn.seeker.utils.captchautil; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet(urlpatterns = "/captcha.png") public class captchaservlet extends httpservlet { @override protected void service(httpservletrequest request, httpservletresponse response) throws servletexception, ioexception { response.setcontenttype("image/png"); //获取随机字符串 string random = captchautil.random(); //输出图片 captchautil.outputimage(random,response.getoutputstream()); //存入结果 request.getsession().setattribute("res",captchautil.num); } }

registservlet.jsp

package cn.seeker.controller; import javax.servlet.servletexception; import javax.servlet.annotation.webservlet; import javax.servlet.http.httpservlet; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; @webservlet(urlpatterns = "/regist") public class registservlet extends httpservlet { @override protected void service(httpservletrequest req, httpservletresponse resp) throws servletexception, ioexception { string cap = req.getparameter("cap"); integer res = (integer)req.getsession().getattribute("res"); if(integer.parseint(cap) == res) { // 填写正确,注册成功 req.getsession().removeattribute("res"); system.out.println("注册成功"); resp.sendredirect("/index.jsp"); } else { req.setattribute("error", "验证码不正确"); req.getrequestdispatcher("/jsp/regist.jsp").forward(req,resp); } } }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

展开全文
内容来源于互联网和用户投稿,文章中一旦含有米乐app官网登录的联系方式务必识别真假,本站仅做信息展示不承担任何相关责任,如有侵权或涉及法律问题请联系米乐app官网登录删除

最新文章

网站地图