java生成验证码,可刷新

来源:互联网 发布:国信软件 编辑:程序博客网 时间:2024/06/07 01:27

code.jsp

Html代码  收藏代码
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>  
  5. <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>  
  6. <%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>  
  7. <%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>  
  8. <html>  
  9. <head>  
  10. < http-equivhttp-equiv="Content-Type" content="text/html; charset=UTF-8">  
  11. <title>验证码</title>  
  12.   
  13. </head>  
  14. <body>  
  15.   
  16.   
  17. <html:img page="/code.do" border="0" 这里写单击事件onclic  k="this.src='/lookctrl/code.do'" alt="请输入此验证码,如看不清请点击刷新。" style="cursor:pointer" />  
  18.   
  19.   
  20. </body>  
  21. </html>  

 CodeAction.java

Java代码  收藏代码
  1. package struts.action;  
  2.   
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.image.BufferedImage;  
  7. import java.util.Random;  
  8.   
  9. import javax.imageio.ImageIO;  
  10. import javax.servlet.ServletOutputStream;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpServletResponse;  
  13. import javax.servlet.http.HttpSession;  
  14.   
  15. import org.apache.struts.action.Action;  
  16. import org.apache.struts.action.ActionForm;  
  17. import org.apache.struts.action.ActionForward;  
  18. import org.apache.struts.action.ActionMapping;  
  19. import org.apache.commons.lang.RandomStringUtils;  
  20.   
  21. /** 
  22.  * MyEclipse Struts Creation date: 08-2-2008 
  23.  *  
  24.  * XDoclet definition: 
  25.  *  
  26.  * @struts.action validate="true" 
  27.  */  
  28. public class CodeAction extends Action {  
  29.  /* 
  30.   * Generated Methods 
  31.   */  
  32.   
  33.  /** 
  34.   * Method execute 
  35.   *  
  36.   * @param mapping 
  37.   * @param form 
  38.   * @param request 
  39.   * @param response 
  40.   * @return ActionForward 
  41.   */  
  42.  public ActionForward execute(ActionMapping mapping, ActionForm form,  
  43.    HttpServletRequest request, HttpServletResponse response) {  
  44.   try {  
  45.    int width = 50;  
  46.    int height = 18;  
  47.    // 取得一个4位随机字母数字字符串  
  48.    String s = RandomStringUtils.random(4truetrue);//这个s的值就是页面验证码上显示的值  
  49.      
  50.    // 保存入session,用于与用户的输入进行比较.  
  51.    // 注意比较完之后清除session.  
  52.    HttpSession session = request.getSession(true);  
  53.    session.setAttribute("validateCode", s);  
  54.   
  55.    response.setContentType("images/jpeg");  
  56.    response.setHeader("Pragma""No-cache");  
  57.    response.setHeader("Cache-Control""no-cache");  
  58.    response.setDateHeader("Expires"0);  
  59.      
  60.      
  61.    ServletOutputStream out = response.getOutputStream();  
  62.    BufferedImage image = new BufferedImage(width, height,  
  63.      BufferedImage.TYPE_INT_RGB);  
  64.    Graphics g = image.getGraphics();  
  65.    // 设定背景色  
  66.    g.setColor(getRandColor(200250));  
  67.    g.fillRect(00, width, height);  
  68.   
  69.    // 设定字体  
  70.    Font mFont = new Font("Times New Roman", Font.BOLD, 18);// 设置字体  
  71.    g.setFont(mFont);  
  72.   
  73.    // 画边框  
  74.    // g.setColor(Color.BLACK);  
  75.    // g.drawRect(0, 0, width - 1, height - 1);  
  76.   
  77.    // 随机产生干扰线,使图象中的认证码不易被其它程序探测到  
  78.    g.setColor(getRandColor(160200));  
  79.    // 生成随机类  
  80.    Random random = new Random();  
  81.    for (int i = 0; i < 155; i++) {  
  82.     int x2 = random.nextInt(width);  
  83.     int y2 = random.nextInt(height);  
  84.     int x3 = random.nextInt(12);  
  85.     int y3 = random.nextInt(12);  
  86.     g.drawLine(x2, y2, x2 + x3, y2 + y3);  
  87.    }  
  88.   
  89.    // 将认证码显示到图象中  
  90.    g.setColor(new Color(20 + random.nextInt(110), 20 + random  
  91.      .nextInt(110), 20 + random.nextInt(110)));  
  92.   
  93.    g.drawString(s, 216);  
  94.   
  95.    // 图象生效  
  96.    g.dispose();  
  97.    // 输出图象到页面  
  98.    ImageIO.write((BufferedImage) image, "JPEG", out);  
  99.    out.close();  
  100.   } catch (Exception e) {  
  101.    e.printStackTrace();  
  102.   }  
  103.   return null;  
  104.  }  
  105.   
  106.  private Color getRandColor(int fc, int bc) { // 给定范围获得随机颜色  
  107.   Random random = new Random();  
  108.   if (fc > 255)  
  109.    fc = 255;  
  110.   if (bc > 255)  
  111.    bc = 255;  
  112.   int r = fc + random.nextInt(bc - fc);  
  113.   int g = fc + random.nextInt(bc - fc);  
  114.   int b = fc + random.nextInt(bc - fc);  
  115.   return new Color(r, g, b);  
  116.  }  

原创粉丝点击