1. 程式人生 > >JAVA實現通過繫結郵箱找回密碼功能

JAVA實現通過繫結郵箱找回密碼功能

1.輸入使用者名稱及驗證碼,驗證使用者名稱是否存在

(1).生成驗證碼工具類

  1. package com.utils;    
  2. import java.awt.Color;    
  3. import java.awt.Font;    
  4. import java.awt.Graphics;    
  5. import java.awt.image.BufferedImage;    
  6. import java.util.HashMap;    
  7. import java.util.Map;    
  8. import java.util.Random;    
  9. /**    
  10.  * @Title: GraphicsUtil.java  
  11.  * @copyright   
  12.  * @Package com.utils  
  13.  * @Description: TODO(這裡用一句話描述這個類的作用)  
  14.  * @author Mr.chen  
  15.  * @date 2016-11-2 下午03:31:30  
  16.  */
  17. publicclass GraphicsUtil {    
  18.     private Font mFont = new Font("Times New Roman", Font.PLAIN, 17);     
  19.     Color getRandColor(int
     fc,int bc){    
  20.         Random random = new Random();         
  21.         if(fc>255) fc=255;         
  22.         if(bc>255) bc=255;         
  23.         int r=fc+random.nextInt(bc-fc);         
  24.         int g=fc+random.nextInt(bc-fc);         
  25.         int b=fc+random.nextInt(bc-fc);         
  26.         return
    new Color(r,g,b);    
  27.     }    
  28.     public Map<String, Object> getGraphics(){    
  29.         int width=100,height=18;    
  30.         BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);    
  31.         Graphics g=image.getGraphics();    
  32.         Random random = new Random();         
  33.         g.setColor(getRandColor(200,250));         
  34.         g.fillRect(11, width-1, height-1);         
  35.         g.setColor(new Color(102,102,102));         
  36.         g.drawRect(00, width-1, height-1);         
  37.         g.setFont(mFont);         
  38.         g.setColor(getRandColor(160,200));         
  39.         //畫隨機線       
  40.         for (int i=0;i<155;i++){         
  41.             int x = random.nextInt(width - 1);         
  42.             int y = random.nextInt(height - 1);         
  43.             int xl = random.nextInt(6) + 1;         
  44.             int yl = random.nextInt(12) + 1;         
  45.             g.drawLine(x,y,x + xl,y + yl);         
  46.         }         
  47.         //從另一方向畫隨機線       
  48.         for (int i = 0;i < 70;i++){         
  49.             int x = random.nextInt(width - 1);         
  50.             int y = random.nextInt(height - 1);         
  51.             int xl = random.nextInt(12) + 1;         
  52.             int yl = random.nextInt(6) + 1;         
  53.             g.drawLine(x,y,x - xl,y - yl);         
  54.         }         
  55.         //生成隨機數,並將隨機數字轉換為字母       
  56.         String sRand="";         
  57.         for (int i=0;i<6;i++){         
  58.             int itmp = random.nextInt(26) + 65;         
  59.             char ctmp = (char)itmp;         
  60.             sRand += String.valueOf(ctmp);         
  61.             g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));         
  62.             g.drawString(String.valueOf(ctmp),15*i+10,16);         
  63.         }         
  64.         g.dispose();    
  65.         Map<String, Object> map=new HashMap<String, Object>();    
  66.         map.put("rand", sRand);    
  67.         map.put("image", image);    
  68.         return map;    
  69.      }    
  70. }  


(2).生成驗證碼action
  1. /**  
  2. * @Description: 獲取驗證碼   
  3. * @author Mr.chen  
  4. * @date 2016-11-2 下午03:45:28  
  5.  */
  6. publicvoid getCode(){    
  7.     try {    
  8.         HttpServletResponse response = ServletActionContext.getResponse();    
  9.         HttpServletRequest request= ServletActionContext.getRequest();    
  10.         response.setHeader("Pragma","No-cache");         
  11.         response.setHeader("Cache-Control","no-cache");         
  12.         response.setDateHeader("Expires"0);         
  13.