Servlet生成隨機驗證碼

 

  1. package book.servlet;  
  2.  
  3. import java.awt.Color;  
  4. import java.awt.Font;  
  5. import java.awt.Graphics;  
  6. import java.awt.p_w_picpath.BufferedImage;  
  7. import java.io.IOException;  
  8. import java.util.Random;  
  9.  
  10. import javax.p_w_picpathio.ImageIO;  
  11. import javax.servlet.ServletException;  
  12. import javax.servlet.http.HttpServlet;  
  13. import javax.servlet.http.HttpServletRequest;  
  14. import javax.servlet.http.HttpServletResponse;  
  15.  
  16. /**  
  17.  * 生成圖形驗證碼的Servlet  
  18.  */ 
  19. public class ImageCodeMakerServlet extends HttpServlet {  
  20.  
  21.     public void doGet(HttpServletRequest req, HttpServletResponse resp)  
  22.             throws ServletException, IOException {  
  23.         doPost(req, resp);  
  24.     }  
  25.  
  26.     /**  
  27.      * @see javax.servlet.http.HttpServlet#void  
  28.      *      (javax.servlet.http.HttpServletRequest,  
  29.      *      javax.servlet.http.HttpServletResponse)  
  30.      */ 
  31.     public void doPost(HttpServletRequest req, HttpServletResponse resp)  
  32.             throws ServletException, IOException {  
  33.  
  34.         System.out.println("和峨峨峨峨峨峨峨峨峨峨");  
  35.         // 首先設置頁面不緩存  
  36.         resp.setHeader("Pragma""No-cache");  
  37.         resp.setHeader("Cache-Control""no-cache");  
  38.         resp.setDateHeader("Expires"0);  
  39.  
  40.         // 定義圖片的寬度和高度  
  41.         int width = 90, height = 40;  
  42.         // 創建一個圖像對象  
  43.         BufferedImage p_w_picpath = new BufferedImage(width, height,  
  44.                 BufferedImage.TYPE_INT_RGB);  
  45.         // 得到圖像的環境對象  
  46.         Graphics g = p_w_picpath.createGraphics();  
  47.  
  48.         Random random = new Random();  
  49.         // 用隨機顏色填充圖像背景  
  50.         g.setColor(getRandColor(180250));  
  51.         g.fillRect(00, width, height);  
  52.         for (int i = 0; i < 5; i++) {  
  53.             g.setColor(getRandColor(50100));  
  54.             int x = random.nextInt(width);  
  55.             int y = random.nextInt(height);  
  56.             g.drawOval(x, y, 44);  
  57.         }  
  58.         // 設置字體,下面準備畫隨機數  
  59.         g.setFont(new Font("", Font.PLAIN, 40));  
  60.         // 隨機數字符串  
  61.         String sRand = "";  
  62.         for (int i = 0; i < 4; i++) {  
  63.             // 生成四個數字字符  
  64.             String rand = String.valueOf(random.nextInt(10));  
  65.             sRand += rand;  
  66.             // 生成隨機顏色  
  67.             g.setColor(new Color(20 + random.nextInt(80), 20 + random  
  68.                     .nextInt(100), 20 + random.nextInt(90)));  
  69.             // 將隨機數字畫在圖像上  
  70.             g.drawString(rand, (17 + random.nextInt(3)) * i + 834);  
  71.  
  72.             // 生成干擾線  
  73.             for (int k = 0; k < 12; k++) {  
  74.                 int x = random.nextInt(width);  
  75.                 int y = random.nextInt(height);  
  76.                 int xl = random.nextInt(9);  
  77.                 int yl = random.nextInt(9);  
  78.                 g.drawLine(x, y, x + xl, y + yl);  
  79.             }  
  80.         }  
  81.  
  82.         // 將生成的隨機數字字符串寫入Session  
  83.         req.getSession().setAttribute("randcode", sRand);  
  84.         // 使圖像生效  
  85.         g.dispose();  
  86.         // 輸出圖像到頁面  
  87.         ImageIO.write(p_w_picpath, "JPEG", resp.getOutputStream());  
  88.     }  
  89.  
  90.     /**  
  91.      * 產生一個隨機的顏色  
  92.      * @param fc    顏色分量最小值  
  93.      * @param bc    顏色分量最大值  
  94.      * @return  
  95.      */ 
  96.     public Color getRandColor(int fc, int bc) {  
  97.         Random random = new Random();  
  98.         if (fc > 255){  
  99.             fc = 255;  
  100.         }  
  101.         if (bc > 255){  
  102.             bc = 255;  
  103.         }  
  104.         int r = fc + random.nextInt(bc - fc);  
  105.         int g = fc + random.nextInt(bc - fc);  
  106.         int b = fc + random.nextInt(bc - fc);  
  107.         return new Color(r, g, b);  
  108.     }  

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章