struts2 驗證碼

import java.awt.Color;
004	import java.awt.Font;
005	import java.awt.Graphics;
006	import java.awt.image.BufferedImage;
007	import java.util.Random;
008	 
009	import javax.imageio.ImageIO;
010	import javax.servlet.http.HttpServletResponse;
011	 
012	import org.apache.struts2.ServletActionContext;
013	 
014	public class CheckAction {
015	 
016	     
017	 
018	 
019	    public String exec() throws Exception {
020	 
021	       HttpServletResponse response = ServletActionContext.getResponse();
022	       int codeLength = 4;// 驗證碼長度
023	       int mixTimes = 1;// 模糊程度參數
024	       Color bgColor = getRandColor(200, 250);// 背景顏色
025	       Color bfColor = new Color(0, 0, 0);// 字體顏色
026	       boolean ifRandomColor = true;// 單個字符是否顏色隨機
027	       boolean ifMixColor = false;// 模糊線是否顏色隨機
028	 
029	       // 設置頁面不緩存
030	       response.setHeader("Pragma", "No-cache");
031	       response.setHeader("Cache-Control", "no-cache");
032	       response.setDateHeader("Expires", 0);
033	       // 在內存中創建圖象
034	       int width = 13 * codeLength + 6, height = 20;
035	       BufferedImage image = new BufferedImage(width, height,
036	         BufferedImage.TYPE_INT_RGB);
037	       // 獲取圖形上下文
038	       Graphics g = image.getGraphics();
039	       // 設定背景色
040	       g.setColor(bgColor);
041	       g.fillRect(0, 0, width, height);
042	       // 設定字體
043	       g.setFont(new Font("Arail", Font.BOLD, 18));
044	       // 畫邊框
045	       g.setColor(new Color(33, 66, 99));
046	       g.drawRect(0, 0, width - 1, height - 1);
047	       // 隨機產生干擾線,使圖象中的認證碼不易被其它程序探測到
048	       g.setColor(getRandColor(160, 200));
049	       for (int i = 0; i < mixTimes * codeLength / 10; i++) {
050	        if (ifMixColor) {
051	         g.setColor(getRandColor(160, 200));
052	        }
053	        int x = random.nextInt(width);
054	        int y = random.nextInt(height);
055	        int xl = random.nextInt(12);
056	        int yl = random.nextInt(12);
057	        g.drawLine(x, y, x + xl, y + yl);
058	       }
059	       // 取隨機產生的認證碼(4位數字)
060	        
061	    // 向圖片中輸出數字和字母
062	        Random  r=new Random();
063	       StringBuffer sb = new StringBuffer();
064	 
065	       char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
066	 
067	       int index, len = ch.length;
068	       String sRand = "";
069	       for (int i = 0; i < codeLength; i ++) {
070	 
071	              index = r.nextInt(len);
072	      
073	        String rand =String.valueOf(ch[index]);
074	        sRand += rand;
075	        // 將認證碼顯示到圖象中
076	        if (ifRandomColor)
077	         g.setColor(getRandColor(20, 110, 0));
078	        else
079	         g.setColor(bfColor);
080	        // 調用函數出來的顏色相同,可能是因爲種子太接近,所以只能直接生成
081	        g.drawString(rand, 13 * i + 6, 16);
082	       }
083	       // 將認證碼存入SESSION
084	       // 圖象生效
085	       ServletActionContext.getRequest().getSession().setAttribute("rand",
086	         sRand);
087	       g.dispose();
088	       // 輸出圖象到頁面
089	       ImageIO.write(image, "PNG", response.getOutputStream());
090	       return  "success";
091	 
092	    }
093	 
094	    private static Random random = new Random();
095	 
096	    private Color getRandColor(int fc, int bc) {
097	       return getRandColor(fc, bc, fc);
098	    }
099	 
100	    private Color getRandColor(int fc, int bc, int interval) {
101	       if (fc > 255) {
102	        fc = 255;
103	       }
104	       if (bc > 255) {
105	        bc = 255;
106	       }
107	       int r = fc + random.nextInt(bc - interval);
108	       int g = fc + random.nextInt(bc - interval);
109	       int b = fc + random.nextInt(bc - interval);
110	       return new Color(r, g, b);
111	    }
112	     
113	}
[代碼] [Java]代碼
001	package org.lsf.action;
002	 
003	import java.awt.Color;
004	import java.awt.Font;
005	import java.awt.Graphics;
006	import java.awt.image.BufferedImage;
007	import java.util.Random;
008	 
009	import javax.imageio.ImageIO;
010	import javax.servlet.http.HttpServletResponse;
011	 
012	import org.apache.struts2.ServletActionContext;
013	 
014	public class CheckAction {
015	 
016	     
017	 
018	 
019	    public String exec() throws Exception {
020	 
021	       HttpServletResponse response = ServletActionContext.getResponse();
022	       int codeLength = 4;// 驗證碼長度
023	       int mixTimes = 1;// 模糊程度參數
024	       Color bgColor = getRandColor(200, 250);// 背景顏色
025	       Color bfColor = new Color(0, 0, 0);// 字體顏色
026	       boolean ifRandomColor = true;// 單個字符是否顏色隨機
027	       boolean ifMixColor = false;// 模糊線是否顏色隨機
028	 
029	       // 設置頁面不緩存
030	       response.setHeader("Pragma", "No-cache");
031	       response.setHeader("Cache-Control", "no-cache");
032	       response.setDateHeader("Expires", 0);
033	       // 在內存中創建圖象
034	       int width = 13 * codeLength + 6, height = 20;
035	       BufferedImage image = new BufferedImage(width, height,
036	         BufferedImage.TYPE_INT_RGB);
037	       // 獲取圖形上下文
038	       Graphics g = image.getGraphics();
039	       // 設定背景色
040	       g.setColor(bgColor);
041	       g.fillRect(0, 0, width, height);
042	       // 設定字體
043	       g.setFont(new Font("Arail", Font.BOLD, 18));
044	       // 畫邊框
045	       g.setColor(new Color(33, 66, 99));
046	       g.drawRect(0, 0, width - 1, height - 1);
047	       // 隨機產生干擾線,使圖象中的認證碼不易被其它程序探測到
048	       g.setColor(getRandColor(160, 200));
049	       for (int i = 0; i < mixTimes * codeLength / 10; i++) {
050	        if (ifMixColor) {
051	         g.setColor(getRandColor(160, 200));
052	        }
053	        int x = random.nextInt(width);
054	        int y = random.nextInt(height);
055	        int xl = random.nextInt(12);
056	        int yl = random.nextInt(12);
057	        g.drawLine(x, y, x + xl, y + yl);
058	       }
059	       // 取隨機產生的認證碼(4位數字)
060	        
061	    // 向圖片中輸出數字和字母
062	        Random  r=new Random();
063	       StringBuffer sb = new StringBuffer();
064	 
065	       char[] ch = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
066	 
067	       int index, len = ch.length;
068	       String sRand = "";
069	       for (int i = 0; i < codeLength; i ++) {
070	 
071	              index = r.nextInt(len);
072	      
073	        String rand =String.valueOf(ch[index]);
074	        sRand += rand;
075	        // 將認證碼顯示到圖象中
076	        if (ifRandomColor)
077	         g.setColor(getRandColor(20, 110, 0));
078	        else
079	         g.setColor(bfColor);
080	        // 調用函數出來的顏色相同,可能是因爲種子太接近,所以只能直接生成
081	        g.drawString(rand, 13 * i + 6, 16);
082	       }
083	       // 將認證碼存入SESSION
084	       // 圖象生效
085	       ServletActionContext.getRequest().getSession().setAttribute("rand",
086	         sRand);
087	       g.dispose();
088	       // 輸出圖象到頁面
089	       ImageIO.write(image, "PNG", response.getOutputStream());
090	       return  "success";
091	 
092	    }
093	 
094	    private static Random random = new Random();
095	 
096	    private Color getRandColor(int fc, int bc) {
097	       return getRandColor(fc, bc, fc);
098	    }
099	 
100	    private Color getRandColor(int fc, int bc, int interval) {
101	       if (fc > 255) {
102	        fc = 255;
103	       }
104	       if (bc > 255) {
105	        bc = 255;
106	       }
107	       int r = fc + random.nextInt(bc - interval);
108	       int g = fc + random.nextInt(bc - interval);
109	       int b = fc + random.nextInt(bc - interval);
110	       return new Color(r, g, b);
111	    }
112	     
113	}
114	//jsp頁面代碼
115	<INPUT id="yanzhen" class=regtxt style="width:60px;" title=請輸入驗證碼  maxLength=5 size=16
116	                   name="yanzhen" > <a href="javascript:reloadVerifyCode()"><img src="<%=request.getContextPath()%>/checks_exec" id="safecode" /></a>
117	//點擊換圖片js代碼
118	 
119	function reloadVerifyCode(){
120	  
121	                            var timenow = new Date().getTime();      
122	                                           
123	                            document.getElementById("safecode").src="<%=request.getContextPath()%>/checks_exec?d="+timenow;
124	                  


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