您的位置:首页 > 编程语言 > Java开发

struts中验证码的实现

2012-04-11 22:55 190 查看
public class ValidateCodeAction {
private ByteArrayInputStream byteArrayInputStream;

public ByteArrayInputStream getByteArrayInputStream() {
return byteArrayInputStream;
}

public void setByteArrayInputStream(
ByteArrayInputStream byteArrayInputStream) {
this.byteArrayInputStream = byteArrayInputStream;
}

public String execute() {
try {
setByteArrayInputStream(createValidateCodeImage());
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}

/**
* 产生一个验证码图片
*
* @return ByteArrayInputStream
*/
private ByteArrayInputStream createValidateCodeImage() {
BufferedImage bufferedImage = new BufferedImage(100, 30,
BufferedImage.TYPE_INT_RGB);
Graphics g = bufferedImage.getGraphics();
g.setColor(Color.white);
g.fillRect(0, 0, 100, 30);
drawBackground(g);// 绘制图片背景
drawValidateCode(g);// 绘制生成的验证码到图片上
ByteArrayInputStream byteArrayInputStream = null;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ImageIO.write(bufferedImage, "JPEG", byteArrayOutputStream);
} catch (IOException e) {
e.printStackTrace();
System.out.println("产生一个验证码图片");
}
byte[] buf = byteArrayOutputStream.toByteArray();
byteArrayInputStream = new ByteArrayInputStream(buf);
try {
byteArrayOutputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return byteArrayInputStream;
}

/**
* 绘制图片背景
*
* @param g
* */
private void drawBackground(Graphics g) {
Random random = new Random();
int randomX, randomY;
for (int i = 0; i < random.nextInt(50)+500; ++i) {
g.setColor(new Color(random.nextInt(255), random.nextInt(255),
random.nextInt(255)));
randomX = random.nextInt(100);
randomY = random.nextInt(30);
g.drawLine(randomX, randomY, randomX, randomY);
}
}

/**
* 产生随机验证码,并存入Session发送至服务器端
*
* @return String
* */
private String createCode() {
Random random = new Random();
StringBuffer sb = new StringBuffer(
"ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890");// 验证码范围
StringBuffer code = new StringBuffer();
int codeLength = 4;// 验证码长度
for (int i = 0; i < codeLength; ++i) {
code.append(sb.charAt(random.nextInt(sb.length())));
}
// 将验证码存入Session
ServletActionContext.getRequest().getSession().setAttribute(
Constant.VALIDATE_CODE, code.toString());
return code.toString();
}

/**
* 将验证码绘制到图片上
*
* @param g
* */
private void drawValidateCode(Graphics g) {
String code = createCode();
Random random = new Random();
int x = 10, y = 0;
g.setFont(new Font("Times New Roman", Font.PLAIN, 30));
for (int i = 0; i < code.length(); ++i) {
y =30-random.nextInt(10);
g.setColor(new Color(random.nextInt(150), random.nextInt(150),
random.nextInt(150)));
g.drawString(code.substring(i, i + 1), x, y);
x += 20;
}
}
}


注意在配置文件中这样配置:

<!-- 验证码解析XML -->
<action name="validateCodeAction" class="com.weiyi.exam.web.action.ValidateCodeAction">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">byteArrayInputStream</param>
</result>
</action>


每次需要验证码的时候,只要请求此Action

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息