日期:2014-05-17  浏览次数:20788 次

struts2 图片验证码
第一次做java项目现在遇到了图片验证码的问题 点击图片更新验证码 点击提交的时候验证
、现在不知道怎么做 大家还给出个主意最好有实例!我要的是struts2 ,action , jsp. 不要servlet的只要这三个的配合的
我说的不是很专业 就是在struts。xml配置好action的那种 大家帮帮忙!

------解决方案--------------------
图片验证码网上还不是大把啊,你不想用人家写的就用现成的组件jcaptcha
------解决方案--------------------
1、action类(相关的包省略)
public class ValidationCodeAction implements Action, SessionAware {
private Map<String, String> session;

public void setSession(Map session) {
this.session = session;
}

// 验证码图片的宽度。
private static final int WIDTH = 60;
// 验证码图片的高度。
private static final int HEIGHT = 20;
// 验证码的数量。
private static final int CODE_AMOUNT = 4;

// 验证码序列。
private static final char[] randomSequence = new char[] { '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' };

// 保存验证码图像数据的字节数组
private byte[] imageBytes;

/**
 * 给定范围内获得随机颜色
 * 
 * @param fc
 * @param bc
 * @return
 */
private Color getRangeColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 200;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}

/**
 * 产生验证码图像,将图像数据保存到字节数组中
 * 
 * @throws ImageFormatException
 * @throws IOException
 */