本文共 6412 字,大约阅读时间需要 21 分钟。
首先需要一个验证码生成的工具类(ConfirmCodeGen.java):
package com.tools;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.util.Random;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;/** * 生成验证码 * * @author Michael Young * */public final class ConfirmCodeGen { /** * @param response * @param width * @param height * @param len * @return * @throws Exception */ public static String genImageCode(HttpServletResponse response, int width, int height, int len) throws Exception { // random number String code = Integer.toString((int) (Math.random() * (1E4))); for (int i = code.length(); i < len; i++) code += "0"; response.setContentType("image/jpeg"); response.addHeader("pragma", "NO-cache"); response.addHeader("Cache-Control", "no-cache"); response.addDateHeader("Expries", 0); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); // 以下填充背景颜色 g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); // 画边框 g.setColor(Color.black); g.drawRect(0, 0, width - 1, height - 1); // 设置字体颜色 g.setColor(Color.BLACK); // g.setColor(new Color()); g.setFont(new Font("Times New Roman", Font.BOLD, 16)); g.drawString(code, 5, 15); // 88点 Random random = new Random(); for (int i = 0; i < 66; i++) { int x = random.nextInt(width); int y = random.nextInt(height); g.setColor(new Color((int) Math.floor(Math.random() * 255))); g.drawLine(x, y, x, y); } g.dispose(); ServletOutputStream outStream = response.getOutputStream(); JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(outStream); encoder.encode(image); outStream.flush(); outStream.close(); return code; } /** * @param session * @param confirmCode * @return */ public static boolean isCorrectCode(HttpServletRequest request) { HttpSession session = request.getSession(); String confirmCode = request.getParameter("confirmCode"); if (session.getAttribute("confirmcode") == null || confirmCode == null) { return false; } String code = (String) session.getAttribute("confirmcode"); // 移除Session session.removeAttribute("confirmcode"); if (code.equals(confirmCode)) { return true; } return false; } /** * 从ajax判断验证码的正确与否 * * @param confirmCode * 验证码 * @param session * @return */ public static Byte isCorrectCodeFromSession(String confirmCode, HttpSession session) { if (session.getAttribute("confirmcode") == null || confirmCode == null) { return 0; } String code = (String) session.getAttribute("confirmcode"); if (code.equals(confirmCode)) { return 1; } // 移除Session session.removeAttribute("confirmcode"); return 0; }}
2、需要一个自定义的Action的result,(ConfirmCodeGenResult.java):
/** * */package com.web.struts.results;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import org.apache.struts2.dispatcher.StrutsResultSupport;import com.opensymphony.xwork2.ActionInvocation;import com.tools.ConfirmCodeGen;/** * title: 生成的验证码的Result * * @author conkeyn * @时间 2009-4-14:下午02:31:52 */public class ConfirmCodeGenResult extends StrutsResultSupport { /** */ private static final long serialVersionUID = 2545890371747338210L; /** 随机图片宽度 */ private int c_width; /** 随机图片高度 */ private int c_height; /** 随机数长度 */ private int c_length; @Override protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); Object widthO = invocation.getStack().findValue("c_width"); if (widthO != null) { c_width = (Integer) widthO; if (c_width <= 0) { c_width = 45; } } else { c_width = 45; } Object heightO = invocation.getStack().findValue("c_height"); if (heightO != null) { c_height = (Integer) heightO; if (c_height <= 0) { c_height = 20; } } else { c_height = 20; } Object c_lengthO = invocation.getStack().findValue("c_length"); if (c_lengthO != null) { c_length = (Integer) c_lengthO; if (c_length <= 0) { c_length = 4; } } else { c_length = 4; } String random = ConfirmCodeGen.genImageCode(response, c_width, c_height, c_length); session.setAttribute("confirmcode", random); } public int getC_width() { return c_width; } public void setC_width(int c_width) { this.c_width = c_width; } public int getC_height() { return c_height; } public void setC_height(int c_height) { this.c_height = c_height; } public int getC_length() { return c_length; } public void setC_length(int c_length) { this.c_length = c_length; }}
3、需要一个默认的action():
package com.web.struts.action;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;/** * 基本Action * * * */public class base extends ActionSupport { /** * */ private static final long serialVersionUID = -7546749187705748430L; /** 验证码的随机图片宽度 */ protected int c_width; /** 验证码的随机图片高度 */ protected int c_height; /** 验证码的随机数长度 */ protected int c_length; public String execute() throws Exception { return SUCCESS; } /** * Convenience method to get the request * * @return current request */ protected HttpServletRequest getRequest() { return ServletActionContext.getRequest(); } /** * Convenience method to get the response * * @return current response */ protected HttpServletResponse getResponse() { return ServletActionContext.getResponse(); } /** * Convenience method to get the session */ protected HttpSession getSession() { return getRequest().getSession(); } public int getC_width() { return c_width; } public void setC_width(int c_width) { this.c_width = c_width; } public int getC_height() { return c_height; } public void setC_height(int c_height) { this.c_height = c_height; } public int getC_length() { return c_length; } public void setC_length(int c_length) { this.c_length = c_length; }}
配置文件(struts-validation.xml):
转载地址:http://czdhb.baihongyu.com/