基於RESTful實現 圖片驗證碼


定義RESTful服務

public class AirResourceConfig extends ResourceConfig{
    public AirResourceConfig() {
            packages("resource");
            register(RequestContextFilter.class);
        }  
}

驗證碼資源

@Path("captcha")
public class CaptchaResource {
    
    private final int width = 90;//定義圖片的width
    private final int height = 20;//定義圖片的height
    private final int codeCount = 4;//定義圖片上顯示驗證碼的個數
    private final int xx = 15;
    private final int fontHeight = 18;
    private final int codeY = 16;
    char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
            'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
            'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
    
    @GET
    public void getCode(@Context HttpServletResponse resp) throws IOException{// 定義圖像buffer
        BufferedImage buffImg = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
//      Graphics2D gd = buffImg.createGraphics();
        //Graphics2D gd = (Graphics2D) buffImg.getGraphics();
        Graphics gd = buffImg.getGraphics();
        // 創建一個隨機數生成器類
        Random random = new Random();
        // 將圖像填充為白色
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);
 
        // 創建字體,字體的大小應該根據圖片的高度來定。
        Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
        // 設置字體。
        gd.setFont(font);
 
        // 畫邊框。
        gd.setColor(Color.BLACK);
        gd.drawRect(0, 0, width - 1, height - 1);
 
        // 隨機產生40條干擾線,使圖象中的認證碼不易被其它程序探測到。
        gd.setColor(Color.BLACK);
        for (int i = 0; i < 40; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            gd.drawLine(x, y, x + xl, y + yl);
        }
 
        // randomCode用於保存隨機產生的驗證碼,以便用戶登錄后進行驗證。
        StringBuffer randomCode = new StringBuffer();
        int red = 0, green = 0, blue = 0;
 
        // 隨機產生codeCount數字的驗證碼。
        for (int i = 0; i < codeCount; i++) {
            // 得到隨機產生的驗證碼數字。
            String code = String.valueOf(codeSequence[random.nextInt(36)]);
            // 產生隨機的顏色分量來構造顏色值,這樣輸出的每位數字的顏色值都將不同。
            red = random.nextInt(255);
            green = random.nextInt(255);
            blue = random.nextInt(255);
 
            // 用隨機產生的顏色將驗證碼繪制到圖像中。
            gd.setColor(new Color(red, green, blue));
            gd.drawString(code, (i + 1) * xx, codeY);
 
            // 將產生的四個隨機數組合在一起。
            randomCode.append(code);
        }
        // 將四位數字的驗證碼保存到Session中。
//        HttpSession session = req.getSession();
//        System.out.print(randomCode);
//        session.setAttribute("code", randomCode.toString());
 
        // 禁止圖像緩存。
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
 
        resp.setContentType("image/jpeg");
 
        // 將圖像輸出到Servlet輸出流中。
        ServletOutputStream sos = resp.getOutputStream();
        ImageIO.write(buffImg, "jpeg", sos);
        sos.close();
    
    }
}

注意:使用 @Context 注入HttpServletResponse 對象

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM