java验证码

1package cn.edu.pdsu.action; 2 3import java.awt.Color; 4import java.awt.Font; 5import java.awt.Graphics; 6import java.awt.image.BufferedImage; 7import java.util.Random; 8 9import javax.imageio.ImageIO; 10import javax.servlet.ServletOutputStream; 11import javax.servlet.http.HttpServletRequest; 12import javax.servlet.http.HttpServletResponse; 13import javax.servlet.http.HttpSession; 14 15import org.apache.commons.lang.RandomStringUtils; 16import org.apache.struts2.ServletActionContext; 17 18/** 19 * 类说明:验证码类(将验证码信息写入到session中 属性“authCode”) 20 * 21 * @author 作者: LiuJunGuang 22 * @version 创建时间:2011-7-17 下午03:26:21 23 */ 24public class AuthCodeAction { 25 private HttpServletResponse response = ServletActionContext.getResponse(); 26 private HttpServletRequest request = ServletActionContext.getRequest(); 27 28 public String execute() { 29 try { 30 int width = 50; 31 int height = 20; 32 // 取得一个4位随机字母数字字符串 33 String s = RandomStringUtils.random(4, true, true); 34 35 // 保存入session,用于与用户的输入进行比较. 36 // 注意比较完之后清除session. 37 HttpSession session = request.getSession(true); 38 session.setAttribute("authCode", s); 39 40 response.setContentType("images/jpeg");//告知浏览器内容的类型 41 response.setHeader("Pragma", "No-cache");//HTTP 1.0版 不要缓存 42 response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1 不要缓存 43 response.setDateHeader("Expires", 0);//设置存活时间 44 45 ServletOutputStream out = response.getOutputStream();//得到响应输出流 46 BufferedImage image = new BufferedImage(width, height, 47 BufferedImage.TYPE_INT_RGB); 48 Graphics g = image.getGraphics(); 49 // 设定背景色 50 g.setColor(getRandColor(200, 250)); 51 g.fillRect(0, 0, width, height); 52 53 // 设定字体 54 Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 设置字体 55 g.setFont(mFont); 56 57 // 画边框 58 // g.setColor(Color.BLACK); 59 // g.drawRect(0, 0, width - 1, height - 1); 60 61 // 随机产生干扰线,使图象中的认证码不易被其它程序探测到 62 g.setColor(getRandColor(160, 200)); 63 // 生成随机类 64 Random random = new Random(); 65 for (int i = 0; i < 125; i++) { 66 int x2 = random.nextInt(width); 67 int y2 = random.nextInt(height); 68 int x3 = random.nextInt(12); 69 int y3 = random.nextInt(12); 70 g.drawLine(x2, y2, x2 + x3, y2 + y3); 71 } 72 // 绘制一些长的干扰线 73 for (int i = 0; i < 5; i++) { 74 int y1 = random.nextInt(15) + 3; 75 g.drawLine(0, y1, width, y1); 76 g.setColor(getRandColor(10, 160)); 77 } 78 79 // 将认证码显示到图象中 80 g.setColor(new Color(20 + random.nextInt(110), 20 + random 81 .nextInt(110), 20 + random.nextInt(110))); 82 83 g.drawString(s, 2, 16); 84 85 // 图象生效 86 g.dispose(); 87 // 输出图象到页面 88 ImageIO.write((BufferedImage) image, "JPEG", out);//将图片以JPEG格式输出到out输出流中 89 out.close(); 90 } catch (Exception e) { 91 e.printStackTrace(); 92 } 93 return null; 94 } 95 96 //获得某一范围的随机颜色 97 private Color getRandColor(int fc, int bc) { // 给定范围获得随机颜色 98 Random random = new Random(); 99 if (fc > 255) 100 fc = 255; 101 if (bc > 255) 102 bc = 255; 103 int r = fc + random.nextInt(bc - fc); 104 int g = fc + random.nextInt(bc - fc); 105 int b = fc + random.nextInt(bc - fc); 106 return new Color(r, g, b); 107 } 108}
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

java游戏开发杂谈

在Eclipse里,编写如下两个类:packagegame2;importjava.awt.Color;importjava.awt.Graphics;importjavax.swing.JPanel;/java游戏开发杂谈demo2:画

java 根据坐标和宽高对图片做剪切

importjava.awt.Color;importjava.awt.Graphics;importjava.awt.Image;importjava.awt.Rectangle;importjava.awt.image.BufferedImage;i

00:Java简单了解

浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。

java验证码 - HelloWorld