1. 程式人生 > >基於web的網上書城系統開發-----登錄註冊擴展-------驗證碼功能

基於web的網上書城系統開發-----登錄註冊擴展-------驗證碼功能

tex puts oid stringbu 圖片 nds 服務器端 raw 輸出流

public class CheckCode extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private int width = 80; // 驗證碼圖片的寬
	private int height = 20; // 驗證碼圖片的高
	private int codeCount = 4; // 驗證碼圖片的字符數
	private int x = 16;
	private int fontHeight = 16;
	private int codeY = 18;
	private final 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‘ };

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, java.io.IOException {
		// 構造一個類型為預定義圖像類型之一的BufferedImage,設置圖像的寬,高,和類型(TYPE_INT_RGB)
		BufferedImage Img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

		// 返回Graphics2D,Graphics2D類擴展自Graphics 類,以提供對幾何形狀、坐標轉換、顏色管理和文本布局更為復雜的控制
		Graphics g = Img.getGraphics();

		Random random = new Random();
		// 將圖像填充為白色
		g.setColor(Color.WHITE);
		// 填充指定的矩形。x,y坐標均為0,寬為width,高為height
		g.fillRect(0, 0, width, height);
		// 創建字體,字體的大小應該根據圖片的高度來定。
		Font font = new Font("Times new Roman", Font.PLAIN, fontHeight);

		g.setColor(Color.black);
		g.setFont(font);
		Color juneFont = new Color(153, 204, 102);
		// 隨機產生130條幹擾線,不易被其它程序探測
		g.setColor(juneFont);
		for (int i = 0; i < 130; i++) {

			// 返回偽隨機數
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(16); // 80/5=16
			int yl = random.nextInt(16);
			// 在此圖形上下文的坐標系中,使用當前顏色在點 (x1, y1) 和 (x2, y2) 之間畫一條線
			g.drawLine(x, y, x + xl, y + yl);

		}
		// randomCode用於保存隨機產生的驗證碼,以便用戶登錄後進行驗證,線程安全的可變字符序列
		StringBuffer randomCode = new StringBuffer();
		// 隨機產生codeCount數字的驗證碼
		for (int i = 0; i < codeCount; i++) {
			// 返回 char 參數的字符串表示形式
			String strRand = String.valueOf(codeSequence[random.nextInt(36)]);

			// 用隨機產生的顏色將驗證碼繪制到圖像中
			// 創建具有指定紅色、綠色和藍色值的不透明的 sRGB 顏色,這些值都在 (0 - 255) 的範圍內
			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));

			// 使用此圖形上下文的當前字體和顏色繪制由指定 string 給定的文本。最左側字符的基線位於此圖形上下文坐標系的 (x, y)
			// 位置處。
			g.drawString(strRand, (i + 1) * x - 4, codeY);
			randomCode.append(strRand);
		}
		
		HttpSession session = request.getSession(); // 將四位數字的驗證碼保存到Session中
		session.setAttribute("realcode", randomCode.toString());
		
		// 禁止瀏覽器緩存
		response.setHeader("Pragma", "no-cache"); // HTTP 1.0
		response.setHeader("Cache-Control", "no-cache");// HTTP 1.1
		response.setDateHeader("Expires", 0); // 在代理服務器端防止緩沖
		response.setContentType("image/gif"); // 設置正被發往客戶端的響應的內容類型

		// 將圖像輸出到Servlet輸出流中,ServletOutputStream提供了向客戶端發送二進制數據的輸出流
		ServletOutputStream sos = response.getOutputStream();
		ImageIO.write(Img, "gif", sos); // 使用支持給定格式的任意 ImageWriter 將一個圖像寫入
										// OutputStream
		sos.flush(); // 刷新此輸出流並強制寫出所有緩沖的輸出字節
		sos.close();
	}

  

基於web的網上書城系統開發-----登錄註冊擴展-------驗證碼功能