温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

JQuery + Struts2 生成验证码

发布时间:2020-07-20 11:21:12 来源:网络 阅读:733 作者:舞_YX 栏目:web开发

 

-- 用于生成验证码

  1. public class RandomNumUtil { 
  2.  
  3.     private ByteArrayInputStream p_w_picpath;// 图像 
  4.  
  5.     private String str;// 验证码 
  6.  
  7.     public RandomNumUtil() { 
  8.         init(); 
  9.     } 
  10.  
  11.     private void init() { 
  12.         // 在内存中创建图象 
  13.         int width = 85, height = 20
  14.         BufferedImage p_w_picpath = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB); 
  15.          
  16.         // 获取图形上下文 
  17.         Graphics g = p_w_picpath.getGraphics(); 
  18.          
  19.         // 生成随机类 
  20.         Random random = new Random(); 
  21.          
  22.         // 设定背景色 
  23.         g.setColor(getRandColor(200250)); 
  24.         g.fillRect(00, width, height); 
  25.          
  26.         // 设定字体 
  27.         g.setFont(new Font("Times New Roman", Font.PLAIN, 12)); 
  28.          
  29.         // 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到 
  30.         g.setColor(getRandColor(160200)); 
  31.         for (int i = 0; i < 155; i++) { 
  32.             int x = random.nextInt(width); 
  33.             int y = random.nextInt(height); 
  34.             int xl = random.nextInt(12); 
  35.             int yl = random.nextInt(12); 
  36.             g.drawLine(x, y, x + xl, y + yl); 
  37.         } 
  38.         // 取随机产生的认证码(6位数字) 
  39.         String sRand = ""
  40.         for (int i = 0; i < 6; i++) { 
  41.             String rand = getVerify(random.nextInt(3)); 
  42.             sRand += rand; 
  43.             // 将认证码显示到图象中 
  44.             g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110))); 
  45.             // 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成 
  46.             g.drawString(rand, 13 * i + 6, random.nextInt(10) + 10); 
  47.         } 
  48.         // 赋值验证码 
  49.         this.str = sRand; 
  50.  
  51.         // 图象生效 
  52.         g.dispose(); 
  53.         ByteArrayInputStream input = null
  54.         ByteArrayOutputStream output = new ByteArrayOutputStream(); 
  55.         try { 
  56.             ImageOutputStream p_w_picpathOut = ImageIO.createImageOutputStream(output); 
  57.             ImageIO.write(p_w_picpath, "JPEG", p_w_picpathOut); 
  58.             p_w_picpathOut.close(); 
  59.             input = new ByteArrayInputStream(output.toByteArray()); 
  60.         } catch (Exception e) { 
  61.             System.out.println("验证码图片产生出现错误:" + e.toString()); 
  62.         } 
  63.  
  64.         this.p_w_picpath = input;/* 赋值图像 */ 
  65.     } 
  66.  
  67.      /* 随机产生颜色 */   
  68.     private Color getRandColor(int fc, int bc) { 
  69.         Random random = new Random(); 
  70.         if (fc > 255
  71.             fc = 255
  72.         if (bc > 255
  73.             bc = 255
  74.         int r = fc + random.nextInt(bc - fc); 
  75.         int g = fc + random.nextInt(bc - fc); 
  76.         int b = fc + random.nextInt(bc - fc); 
  77.         return new Color(r, g, b); 
  78.     } 
  79.      
  80.     /** 
  81.      * 生成验证码 
  82.      * @param i 0为小写字母  
  83.      * 1 为大写字母 
  84.      * 2为普通数字 
  85.      * @return 
  86.      */ 
  87.     private String getVerify(int i){ 
  88.         String str = ""
  89.         if(i == 0){ 
  90.             char c = 'a'
  91.             c=(char)(c+(int)(Math.random()*26)); 
  92.             str = c + ""
  93.         }else if(i == 1){ 
  94.             char c='A'
  95.             c=(char)(c+(int)(Math.random()*26)); 
  96.             str = c + ""
  97.         }else if(i == 2){ 
  98.             str = new Random().nextInt(10) + ""
  99.         } 
  100.         return str; 
  101.     } 
  102.  
  103.     public ByteArrayInputStream getImage() { 
  104.         return p_w_picpath; 
  105.     } 
  106.  
  107.     public void setImage(ByteArrayInputStream p_w_picpath) { 
  108.         this.p_w_picpath = p_w_picpath; 
  109.     } 
  110.  
  111.     public String getStr() { 
  112.         return str; 
  113.     } 
  114.  
  115.     public void setStr(String str) { 
  116.         this.str = str; 
  117.     } 

-- Struts2

  1. public class UtilAction extends ActionSupport { 
  2.  
  3.     private ByteArrayInputStream inputStream;  
  4.      
  5.     @Override 
  6.     public String execute() throws Exception { 
  7.         // TODO Auto-generated method stub 
  8.          RandomNumUtil rdnu = new RandomNumUtil();       
  9.             this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片       
  10.             ActionContext.getContext().getSession().put("validateCode", rdnu.getStr());//取得随机字符串放入HttpSession  
  11.             return SUCCESS;   
  12.     } 
  13.  
  14.     public ByteArrayInputStream getInputStream() { 
  15.         return inputStream; 
  16.     } 
  17.  
  18.     public void setInputStream(ByteArrayInputStream inputStream) { 
  19.         this.inputStream = inputStream; 
  20.     } 

-- JQuery

  1. $(function() { 
  2.     $("#randomCode").attr("src","verify"); 
  3.     $("#refresh").click( 
  4.         function() { 
  5.             $("#randomCode").attr("src","verify"); 
  6.     }); 
  7. }) 

-- Struts2 xml配置

  1. <action name="verify" class="verify.UtilAction"> 
  2.      <result type="stream">    
  3.               <param name="contentType">p_w_picpath/jpeg</param>         
  4.               <param name="inputName">inputStream</param>         
  5.          </result>  
  6. </action> 

-- jsp页面

  1. <body> 
  2.     <img src="" width=150 height="50" alt="验证码图片" id="randomCode" /> 
  3.     <span id="refresh" style="cursor: pointer;">换张图片</span> 
  4. </body> 

--效果如图 

JQuery + Struts2 生成验证码

 

 

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI