隨機生成驗證碼的例子

最近在讀“struts2權威指南”這本書,裏面例子中有段隨機生成驗證碼的代碼,轉帖過來,以後沒準能用到。
雖然現在現成的東西很多,不過還是要多學學代碼,看看作者怎寫的 :-)
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class AuthImg extends HttpServlet ...{

    
// 設置圖形驗證碼中字符串的字體和大小
    private Font mFont = new Font("Arial Black", Font.PLAIN, 16);

    
public void init() throws ServletException ...{
        
super.init();
    }


    
// 生成隨即顏色
    Color getRandColor(int fc, int bc) ...{
        Random random 
= new Random();
        
if (fc > 255)
            fc 
= 255;
        
if (bc > 255)
            bc 
= 255;
        
int r = fc + random.nextInt(bc - fc);
        
int g = fc + random.nextInt(bc - fc);
        
int b = fc + random.nextInt(bc - fc);
        
return new Color(r, g, b);
    }


    
// 生成服務器響應的service方法
    public void service(HttpServletRequest request, HttpServletResponse response)
            
throws ServletException, IOException ...{
        
// 組織生成頁面內容被緩存,保證每次重新生成隨機驗證碼
        response.setHeader("Pragma""No-cache");
        response.setHeader(
"Cache-Control""no-cache");
        response.setDateHeader(
"Expires"0);
        
// 指定 圖形驗證碼大小
        int width = 100, height = 8;
        
// 生成一張新圖片
        BufferedImage image = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        
// 在圖片中繪製內容
        Graphics g = image.getGraphics();
        Random random 
= new Random();
        g.setColor(getRandColor(
200250));
        g.fillRect(
11, width - 1, height - 1);
        g.setColor(
new Color(102102102));
        g.setFont(mFont);
        
// 隨即生成線條,讓圖片看起來更加雜亂
        g.setColor(getRandColor(160200));
        
for (int i = 0; i < 155; i++...{
            
int x1 = random.nextInt(width - 1);
            
int y1 = random.nextInt(height - 1);
            
int x2 = random.nextInt(6+ 1;
            
int y2 = random.nextInt(12+ 1;
            g.drawLine(x1, y1, x2, y2);
        }

        
// 隨機生成線條,讓圖片看起來更雜亂
        for (int i = 0; i < 70; i++...{
            
int x1 = random.nextInt(width - 1);
            
int y1 = random.nextInt(height - 1);
            
int x2 = random.nextInt(6+ 1;
            
int y2 = random.nextInt(12+ 1;
            g.drawLine(x1, y1, x2, y2);
        }

        
// 讀取變量,用於保存系統生成的隨機字符串
        String sRand = "";
        
for (int i = 0; i < 6; i++...{
            
// 取得一個隨機生成的字符
            String tmp = getRandomChar();
            sRand 
+= tmp;
            
// 將系統隨機生成的字符添加到圖形驗證碼圖片上
            g.setColor(new Color(20 + random.nextInt(110), 20 + random
                    .nextInt(
110), 20 + random.nextInt(110)));
            g.drawString(tmp, 
15 * i + 1015);
        }

        
// 取得用戶Session
        HttpSession session = request.getSession(true);
        
// 將系統隨機生成的圖形驗證碼添加到用戶Session中
        session.setAttribute("rand", sRand);
        g.dispose();
        
// 輸出圖形驗證碼圖片
        ImageIO.write(image, "JPEG", response.getOutputStream());
    }


    
private String getRandomChar() ...{
        
int rand = (int) Math.round(Math.random() * 2);
        
long itmp = 0;
        
char ctmp = '';
        
// 根據rand的值來決定是生成小寫字母,大寫字母和數字
        switch (rand) ...{
        
// 生成大寫字母
        case 1:
            itmp 
= Math.round(Math.random() * 25 + 65);
            ctmp 
= (char) itmp;
            
return String.valueOf(ctmp);
            
// 生成小寫字母
        case 2:
            itmp 
= Math.round(Math.random() * 25 + 97);
            ctmp 
= (char) itmp;
            
return String.valueOf(ctmp);
            
// 生成數字
        default:
            itmp 
= Math.round(Math.random() * 9);
            
return String.valueOf(itmp);
        }

    }

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