package com.baizhi.action; import java.io.ByteArrayInputStream; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import cn.hutool.captcha.CaptchaUtil; import cn.hutool.captcha.LineCaptcha; public class CodeAction { private ByteArrayInputStream imageStream; //畫驗證碼,並通過流返回到調用位置,將驗證碼對象放入session中 public String code() { LineCaptcha captcha = CaptchaUtil.createLineCaptcha(150, 50, 4, 6); imageStream = new ByteArrayInputStream(captcha.getImageBytes()); HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("cap", captcha);
//將驗證碼寫進瀏覽器
Response response = ServletActionContext.getResponse();
ServletOutputStream outputStream = response.getOutputStream();
captcha.write(outputStream);
//return "ok";
return null; } public ByteArrayInputStream getImageStream() { return imageStream; } public void setImageStream(ByteArrayInputStream imageStream) { this.imageStream = imageStream; } }
package com.baizhi.action; import javax.servlet.http.HttpSession; import org.apache.struts2.ServletActionContext; import cn.hutool.captcha.LineCaptcha; public class UserAction { private String code; public String login() { //判斷賬號和密碼 //從session中獲取驗證碼對象 HttpSession session = ServletActionContext.getRequest().getSession(); LineCaptcha captcha = (LineCaptcha)session.getAttribute("cap"); //判斷驗證碼是否輸入正確 boolean verify = captcha.verify(code); System.out.println("用戶輸入的驗證碼為:"+verify); return "loginOk"; } public String getCode() { return code; } public void setCode(String code) { this.code = code; } }
package com.baizhi.test; import java.util.Scanner; import cn.hutool.captcha.CaptchaUtil; import cn.hutool.captcha.CircleCaptcha; import cn.hutool.captcha.LineCaptcha; import cn.hutool.captcha.ShearCaptcha; public class CodeTest { public static void main(String[] args) { //使用糊塗工具包畫驗證碼 //CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 80, 4, 100); LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 80, 6, 6); //ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 80, 4, 1); //將驗證碼保存到d://a.jpg captcha.write("D://a.jpg"); Scanner sc = new Scanner(System.in); String next = sc.next(); //判斷用戶輸入是否正確 boolean verify = captcha.verify(next); System.out.println(verify); } }
jsp頁面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath }/user/login"> 賬戶:<input type="text" /><br/> 密碼:<input type="text" /><br/> <img src='${pageContext.request.contextPath}/vericode/Code_showCode' alt="" id="sr" onclick='chance()' height='50px' > <input type="submit" /> </form> </body> </html>
mapper.xml
<package name="code" extends="struts-default"> <action name="code" class="com.baizhi.action.CodeAction" method="code"> <!-- <result name="ok" type="stream"> <param name="contentType">image/jpeg</param> <param name="inputName">imageStream</param> <param name="bufferSize">2048</param> </result> --> </action> </package>
其實利用的是src可以的路徑可以請求action
利用jQuery綁定點擊事件可以實現點擊更換驗證碼
<script type="text/javascript"> function chance() { var img=document.getElementById("sr"); img.src="${pageContext.request.contextPath }/validateCode?a="+ Math.random(); } </script>