天天看點

驗證碼 使用

 下載下傳區 有jar 包

<dependency>
			<groupId>com.github.bingoohuang</groupId>
			<artifactId>patchca</artifactId>
			<version>0.0.1</version>
	</dependency>      

驗證碼 使用

<div class="login_code_pic">
	 <a href="javascript:void(0)">
		 <img src="images/code_demo.png"	id="loginCode" onclick="refreshCode('login');" />
	 </a>
	 </div>
	 <div class="login_code_text">
		 <a href="javascript:void(0)" onclick="refreshCode('login');" >看不清,換一張?</a>
	 </div>
 </div>      
function refreshCode(imgName) {
	var date = new Date();
	try {
		if (imgName == "login") {
			$("#loginCode").attr(
					"src",
					"userAction_refreshPatchcaImage.html?time="
					+ date.getTime());
		} else {
			$("#patchaImage").attr(
					"src",
					"userAction_refreshPatchcaImage.html?time="
					+ date.getTime());
		}
	} catch (e) {
		alert("驗證碼重新整理逾時,請重新重新整理界面!");
		window.location.reload();
	}
}      
private static final String  DEFAULT_CHARACTERS="34578AaCcdEeFGHhJjKkMmNnPpQRsTtUuVvWwXxYy";

private static final String PATCHCACODE = "PATCHCA"; 



public void refreshPatchcaImage() throws Exception {
		HttpServletResponse response = null;
		HttpServletRequest request = null;
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=UTF-8");
		response.setContentType("image/png");//設定相應類型,告訴浏覽器輸出的内容為圖檔        
		response.setHeader("Pragma", "No-cache");//設定響應頭資訊,告訴浏覽器不要緩存此内容        
		response.setHeader("Cache-Control", "no-cache");        
		response.setDateHeader("Expire", 0);
		OutputStream os=null;
		try{
			//建立圖檔對象服務
			ConfigurableCaptchaService cs = new ConfigurableCaptchaService();
			//修改圖檔文字顔色
			cs.setColorFactory(new SingleColorFactory(new Color(0, 0, 0)));
			//建立圖檔文字對象
			RandomWordFactory randomWordFactory = new RandomWordFactory();
			//設定文字對象範圍
			randomWordFactory.setCharacters(DEFAULT_CHARACTERS);
			//設定圖檔文字最大長度
			randomWordFactory.setMaxLength(4);
			//設定圖檔文字最小長度
			randomWordFactory.setMinLength(4);
			//設定圖檔文字屬性
			cs.setWordFactory(randomWordFactory);
			//設定圖檔的幹擾效果的樣式..一共有五種
			cs.setFilterFactory(new WobbleRippleFilterFactory ());
			Captcha captcha = cs.getCaptcha();
			//将四位數字的驗證碼儲存到Session中。
			request.getSession().setAttribute(PATCHCACODE, captcha.getChallenge());
			//将圖像輸出到Servlet輸出流中
			os=response.getOutputStream();
			ImageIO.write(captcha.getImage(), "png", os);
			os.flush();
			//關閉輸出流
		}catch (Exception e) {
			e.printStackTrace();
			/*PrintWriter writer = response.getWriter();
			writer.print(false);
			writer.flush();
			writer.close();*/
			//log.error("生成驗證碼錯誤!......." + e);
		}finally{
			if(null!=os){
				os.close();
			}
		}
		
	}      

捐助開發者

在興趣的驅動下,寫一個

免費

的東西,有欣喜,也還有汗水,希望你喜歡我的作品,同時也能支援一下。 當然,有錢捧個錢場(右上角的愛心标志,支援支付寶和PayPal捐助),沒錢捧個人場,謝謝各位。

驗證碼 使用
驗證碼 使用
驗證碼 使用

 謝謝您的贊助,我會做的更好!

繼續閱讀