JSP生成验证码
来源:互联网 发布:手机淘宝店头像怎么换 编辑:程序博客网 时间:2024/04/30 23:24
这是在网上找到了一片相似的文章,修改后用在自己的工程之中。本来还有一个生成中文验证码的版本,由于保存不当,现在找不到了。思路是:将Character[] codes中的内容改成汉字,当然得用UTF-8的编码。大家可以去网上查汉字对应的UTF-8编码,不过我是用Eclipse的Properties File编辑器,添加汉字,然后查看源码就可以。 比如“张壮壮”就是\u5F20\u58EE\u58EE
<%@ page import="java.util.Random"%><%@ page import="java.io.OutputStream"%><%@ page import="java.awt.Color"%><%@ page import="java.awt.Font"%><%@ page import="java.awt.Graphics"%><%@ page import="java.awt.image.BufferedImage"%><%@ page import="javax.imageio.ImageIO"%><%@ page contentType="image/JPEG"%><%Character[] codes = { '0', '1', '2', '3', '4', '5', '6', '7', '8','9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K','L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W','X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i','j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u','v', 'w', 'x', 'y', 'z' };//定义随机数的个数int codeNum = 5;int width = 120;int height = 30;int fontHeight = height - 2;int codeX = width / (codeNum + 1);int codeY = height - 2;//设置字体//Font font = new Font("Times New Roman", Font.PLAIN, fontHeight); //create the imageBufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();// set the background colorg.setColor(Color.white);g.fillRect(0, 0, width, height);// draw the borderg.setColor(Color.black);g.drawRect(0, 0, width - 1, height - 1);// create a random instance to generate the codesRandom random = new Random();// 随机生产50条图片干扰线条,使验证码图片中的字符不被轻易识别g.setColor(Color.black);for (int i = 0; i < 50; i++) {int x = random.nextInt(width);int y = random.nextInt(height);int xl = random.nextInt(12);int yl = random.nextInt(12);g.drawLine(x, y, x + xl, y + yl);}//定义颜色三素int red = 0;int green = 0;int blue = 0;StringBuffer randomCode = new StringBuffer();//随机生产codeNum个数字验证码for (int i = 0; i < codeNum; i++) {//得到随机产生的验证码String strRand = String.valueOf(codes[random.nextInt(62)]);//使用随机函数产生随机的颜色分量来构造颜色值,这样输出的每位数字的颜色值都将不同。red = random.nextInt(255);green = random.nextInt(255);blue = random.nextInt(255);g.setFont(new Font("Times New Roman", Font.PLAIN, 25));//用随机产生的颜色将验证码绘制到图像中。g.setColor(new Color(red, green, blue));g.drawString(strRand, (i + 1) * codeX, codeY);//将产生的四个随机数组合在一起。randomCode.append(strRand);}session.setAttribute("validate", randomCode.toString());g.dispose();response.setContentType("image/jpeg");out.clear();out = pageContext.pushBody();OutputStream strm = response.getOutputStream();ImageIO.write(image, "jpeg", strm);strm.close();%>
- JSP生成验证码
- JSP生成验证码
- JSP生成验证码
- jsp 生成验证码
- jsp生成验证码
- jsp生成验证码
- jsp生成验证码
- jsp生成验证码
- jsp生成验证码
- jsp 生成验证码
- jsp生成验证码
- JSP生成验证码
- JSP生成验证码
- JSP生成验证码
- JSP生成验证码
- jsp 生成验证码
- JSP生成验证码
- JSP生成验证码
- dedecms导致CPU峰值现象的解决办法
- sql 在sqlplus下能正确执行,但是hibernate 原生sql执行报列名无效错误
- Android多线程任务优化1:探讨AsyncTask的缺陷
- Android 4.1 android.os.NetworkOnMainThreadException
- 安装 TMS Aurelius 2.1 Full Source 遇到的问题和解决办法
- JSP生成验证码
- 开发技巧汇总
- Java学习之路:不走弯路,就是捷径
- 整数划分问题
- hdu_2544 最短路 (floyd算法)
- uva 188 Perfect Hash(进制转换 + 遍历)
- 关于多个div中table的td对齐问题
- 今天碰到Android2.3.3能运行的法度到android4.0中无法运行的景象
- poj-3045 Cow Acrobats