Struts2中的图片验证码

时间:2022-12-30 19:01:31

1.Struts中建一个action

<action name="Code" class="LoginAction" method="code">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">2048</param>
</result></action>

2.写Code.action

首先定义inputStream,并get,set

private ByteArrayInputStream inputStream; 
 public ByteArrayInputStream getInputStream() {
return inputStream;
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}

然后Code.action代码如下

 public String code() throws Exception {

int WIDTH = 60;
int HEIGHT = 20;
HttpServletResponse response = ServletActionContext.getResponse();

// 设置浏览器不要缓存此图片
response.setHeader("Pragma", "no-cache");

response.setHeader("Cache-Control", "no-cache");

response.setDateHeader("Expires", 0);

String str = "0123456789qwertyuiopasdfghjklzxcvbnm";

char[] rand = new char[4];

Random random = new Random();

for (int i = 0; i < 4; i++)
{
rand[i] = str.charAt(random.nextInt(36));
}

String rands =new String(rand);

BufferedImage image = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_RGB);

Graphics g = image.getGraphics();

// 产生图像
// 画背景
g.setColor(new Color(0xDCDCDC));

g.fillRect(0, 0, WIDTH, HEIGHT);

// 随机产生 120 个干扰点

for (int i = 0; i < 120; i++)
{
int x = (int) (Math.random() * WIDTH);

int y = (int) (Math.random() * HEIGHT);

int red = (int) (Math.random() * 255);

int green = (int) (Math.random() * 255);

int blue = (int) (Math.random() * 255);

g.setColor(new Color(red, green, blue));

g.drawOval(x, y, 1, 0);
}

g.setColor(Color.BLACK);

g.setFont(new Font(null, Font.ITALIC | Font.BOLD, 18));

// 在不同的高度上输出验证码的每个字符

g.drawString("" + rands.charAt(0), 1, 17);

g.drawString("" + rands.charAt(1), 16, 15);

g.drawString("" + rands.charAt(2), 31, 18);

g.drawString("" + rands.charAt(3), 46, 16);

System.out.println(rands);

// 结束图像 的绘制 过程, 完成图像
g.dispose();

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

ImageIO.write(image, "jpeg", outputStream);

ByteArrayInputStream input = new ByteArrayInputStream(outputStream.toByteArray());

this.setInputStream(input);

HttpSession session = ServletActionContext.getRequest().getSession();

session.setAttribute("checkCode", rands);

input.close();

outputStream.close();
return SUCCESS;
}

3.jsp中调用

 <form id="form" action="Login.action" method="post">
<table border=0 cellpadding="4">
<tr><td><input type="text" name="username" placeholder="学号/工号/用户名" size="22" style="background-image: url(img/user.JPG);"><br></td></tr>
<tr><td><input type="password" name="password" placeholder="密码" size="22" style="background-image: url(img/password.JPG);"><br></td></tr>
<tr><td>
<input type="text" placeholder="验证码" name="checkCode" size="10"/>
<img alt="" src="Code.action" onclick="this.src='Code.action?'+ Math.random();" title="点击图片刷新验证码">
</td> </tr>
<tr><td><input type="submit" class="btnCheck" value="登录" onclick="validate()" style="width:100%;"></button></td>
</tr>
</table>
</form>

4.在另一个action中验证

HttpSession session = ServletActionContext.getRequest().getSession();    
String checkCode2 = (String)session.getAttribute("checkCode");
System.out.println(checkCode+"aaa"+checkCode2);
if(!checkCode.equals(checkCode2))
{
return "chekCodeerror";
}

5.结果截图

Struts2中的图片验证码