Struts2 图片验证码

本文介绍了一个使用Java实现的随机验证码图片生成方法。该方法通过创建带噪点的图片,并将随机字符绘制到图片上,同时将这些字符保存到session中以便后续验证。文章还展示了如何在Struts2框架中配置和使用此功能。
摘要由CSDN通过智能技术生成

首先当然要导入各种需要的包和jQuery.js生气

  • Action.RandomImg.java
    package Action;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;
    import java.util.Random;
    import javax.imageio.ImageIO;
    import javax.imageio.stream.ImageOutputStream;
    import com.opensymphony.xwork2.ActionContext;
    import com.opensymphony.xwork2.ActionSupport;
    /**
     * Pic validator
     * @author WalkingDog
     *
     */
    public class RandomImg extends ActionSupport
    {
    	private ByteArrayInputStream inputStream;
    	public ByteArrayInputStream getInputStream()
    	{
    		return inputStream;
    	}
    	public void setInputStream(ByteArrayInputStream inputStream)
    	{
    		this.inputStream = inputStream;
    	}
    
    	@Override
    	public String execute() throws Exception
    	{
    		int width = 85, height = 20;
    		BufferedImage image = new  BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    		Graphics g = image.getGraphics();
    		Random random = new Random();
    		g.setColor(getRandColor(200, 255));
    		g.fillRect(0, 0, width, height);
    		
    		g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
    		g.setColor(getRandColor(160, 200));
    		for(int i = 0; i < 155; i++)
    		{
    			int x1 = random.nextInt(width);
    			int y1 = random.nextInt(height);
    			int x2 = random.nextInt(12);
    			int y2 = random.nextInt(12);
    			g.drawLine(x1, y1, x2, y2);
    		}
    		
    		String sRand = "";
    		for(int i = 0; i < 6; i++)
    		{
    			String rand = getRandomChar();
    			sRand += rand;
    			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
    			g.drawString(rand, 13*i+6, 16);
    		}
    		ActionContext.getContext().getSession().put("randomImg", sRand.toLowerCase());//存进session,用于验证
    		g.dispose();
    		ByteArrayOutputStream output = new ByteArrayOutputStream();
    		ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
    		ImageIO.write(image, "JPEG", imageOut);
    		imageOut.close();
    		ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
    		this.setInputStream(input);
    		return SUCCESS;
    		
    	}
    	
    	private Color getRandColor(int fc, int bc)
    	{
    		Random random = new Random();
    		if(fc > 255) fc = 255;
    		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);
    	}
    	private static String getRandomChar()
        {
    		  int rand = (int)Math.round(Math.random() * 2);
    		  long itmp = 0;
    		  char ctmp = '\u0000';
    		  switch (rand)
    		  {
    			   case 1:
    				    itmp = Math.round(Math.random() * 25 + 65);
    				    ctmp = (char)itmp;
    				    return String.valueOf(ctmp);
    			   case 2:
    				    itmp = Math.round(Math.random() * 25 + 97);
    				    ctmp = (char)itmp;
    				    return String.valueOf(ctmp);
    			   default :
    				    itmp = Math.round(Math.random() * 9);
    			    
    		  }
    		  return String.valueOf(itmp);
        }
    }
  • struts.xml
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
        "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    	<package name="struts2" extends="struts-default">
    		<!-- 验证码图片 -->
    		<action name="randomImg" class="Action.RandomImg">
    			<result type="stream">
    				<param name="contentType">image/jpeg</param>
    				<param name="inputtName">inputStream</param>
    			</result>
    		</action>
    
    	</package>
    </struts>

  • randomImg.html
    <html>
    <head>
    <title>randomImg.html</title>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript">
    function chageValidateCode()
    {
    	var timenow = new Date().getTime();
    	$("img.validate").attr("src", "randomImg.action?d="+timenow);
    }
    </script>
    </head>
    <body>
    validateCode:<input type="text" name="validateCode" size="10"/>
    <img class="validate" src="randomImg.action" οnclick="chageValidateCode()"/>
    </body>
    </html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值