struts中验证码的实现
2012-04-11 22:55
190 查看
public class ValidateCodeAction { private ByteArrayInputStream byteArrayInputStream; public ByteArrayInputStream getByteArrayInputStream() { return byteArrayInputStream; } public void setByteArrayInputStream( ByteArrayInputStream byteArrayInputStream) { this.byteArrayInputStream = byteArrayInputStream; } public String execute() { try { setByteArrayInputStream(createValidateCodeImage()); } catch (Exception e) { e.printStackTrace(); } return "success"; } /** * 产生一个验证码图片 * * @return ByteArrayInputStream */ private ByteArrayInputStream createValidateCodeImage() { BufferedImage bufferedImage = new BufferedImage(100, 30, BufferedImage.TYPE_INT_RGB); Graphics g = bufferedImage.getGraphics(); g.setColor(Color.white); g.fillRect(0, 0, 100, 30); drawBackground(g);// 绘制图片背景 drawValidateCode(g);// 绘制生成的验证码到图片上 ByteArrayInputStream byteArrayInputStream = null; ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { ImageIO.write(bufferedImage, "JPEG", byteArrayOutputStream); } catch (IOException e) { e.printStackTrace(); System.out.println("产生一个验证码图片"); } byte[] buf = byteArrayOutputStream.toByteArray(); byteArrayInputStream = new ByteArrayInputStream(buf); try { byteArrayOutputStream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return byteArrayInputStream; } /** * 绘制图片背景 * * @param g * */ private void drawBackground(Graphics g) { Random random = new Random(); int randomX, randomY; for (int i = 0; i < random.nextInt(50)+500; ++i) { g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255))); randomX = random.nextInt(100); randomY = random.nextInt(30); g.drawLine(randomX, randomY, randomX, randomY); } } /** * 产生随机验证码,并存入Session发送至服务器端 * * @return String * */ private String createCode() { Random random = new Random(); StringBuffer sb = new StringBuffer( "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");// 验证码范围 StringBuffer code = new StringBuffer(); int codeLength = 4;// 验证码长度 for (int i = 0; i < codeLength; ++i) { code.append(sb.charAt(random.nextInt(sb.length()))); } // 将验证码存入Session ServletActionContext.getRequest().getSession().setAttribute( Constant.VALIDATE_CODE, code.toString()); return code.toString(); } /** * 将验证码绘制到图片上 * * @param g * */ private void drawValidateCode(Graphics g) { String code = createCode(); Random random = new Random(); int x = 10, y = 0; g.setFont(new Font("Times New Roman", Font.PLAIN, 30)); for (int i = 0; i < code.length(); ++i) { y =30-random.nextInt(10); g.setColor(new Color(random.nextInt(150), random.nextInt(150), random.nextInt(150))); g.drawString(code.substring(i, i + 1), x, y); x += 20; } } }
注意在配置文件中这样配置:
<!-- 验证码解析XML --> <action name="validateCodeAction" class="com.weiyi.exam.web.action.ValidateCodeAction"> <result name="success" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">byteArrayInputStream</param> </result> </action>
每次需要验证码的时候,只要请求此Action
相关文章推荐
- java web编程中 struts2+hibernate 实现B2C电子商务网站的登录 、注册中验证码的实现(源码精简版)
- 基于struts的验证码实现
- Struts实现验证码
- struts2+hibernate 实现B2C电子商务网站的登录 、注册中验证码的实现(源码)
- struts1 登陆验证码实现
- Struts中实现验证码生成和验证
- 【Struts实现页面验证码】
- struts2+hibernate 实现B2C电子商务网站的登录 、注册中验证码的实现(源码)
- struts2+hibernate 实现B2C电子商务网站的登录 、注册中验证码的实现(源码)
- 在struts中实现验证码
- javascript 实现数字验证码
- 简单的验证码实现
- AngularJS 验证码60秒倒计时功能的实现
- java 实现验证码
- PHP之路——验证码实现
- Struts2中表单数据怎样实例化 <input name=user.name>struts实现拿值方法
- PHP实现验证码登录
- php实现生成验证码实例分享
- Android账号注册实现点击获取验证码倒计时效果
- struts如何实现上传文件的重命名????