登录时获取验证码

简单的保存一下获取登录时的验证码    以后我们都可用:

首先:这是一个忘记密码的验证码;

<div class="form-group">
      <label for="lastname" class="col-sm-2 control-label">验证码:</label>
      <div class="col-sm-10">
             <div class="input-group input-group-md">
                    <input type="text" class="form-control" id="lastname" name="lastname" placeholder="请输入验证码">

                    <span class="input-group-addon" style="cursor: pointer;">
                         <img alt="换一张" src="${pageContext.request.contextPath}/getcode" id="validateCodeImg" οnclick="changeImg()">
                    </span>
             </div>
      </div>
</div>

下面是

    //刷新验证码
      function changeImg(){
          document.getElementById("validateCodeImg").src="${pageContext.request.contextPath}/getcode?"+Math.random();
     }

然后跳转到Action层

/**
 * @author Administrator
 * 验证码生成类
 */
@Controller
@RequestMapping
public class SecCodeAction {
    
    @RequestMapping("/getcode")
    public void generate(HttpSession session,HttpServletResponse response){
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        String code = drawImg(output);
        session.setAttribute(Const.SESSION_SECURITY_CODE, code); //放入session
        //System.out.println(session.getAttribute(Const.SESSION_SECURITY_CODE));
        try {
            ServletOutputStream out = response.getOutputStream();
            output.writeTo(out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private String drawImg(ByteArrayOutputStream output){
        String code = "";
        for(int i=0; i<4; i++){
            code += randomChar();
        }
        int width = 70;
        int height = 25;

     // 1.创建一个不带透明色的BufferedImage对象

        BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
        Font font = new Font("Times New Roman",Font.PLAIN,20);
        Graphics2D g = bi.createGraphics();
        g.setFont(font);
        Color color = new Color(66,2,82);
        g.setColor(color);
        g.setBackground(new Color(226,226,240));
        g.clearRect(0, 0, width, height);
        FontRenderContext context = g.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(code, context);
        double x = (width - bounds.getWidth()) / 2;
        double y = (height - bounds.getHeight()) / 2;
        double ascent = bounds.getY();
        double baseY = y - ascent;
        g.drawString(code, (int)x, (int)baseY);
        g.dispose();
        try {
            ImageIO.write(bi, "jpg", output);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return code;
    }
    //获得一个随机的字符  
    private char randomChar(){
        Random r = new Random();//随机类
        String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789"; 
        return s.charAt(r.nextInt(s.length()));
    }
}


r.nextInt(s.length()) 是从0 到s的长度的随机数,

public static final String SESSION_SECURITY_CODE = "sessionSecCode";  //常量值

BufferedImage: 图片的缓冲流,应该可以将图片信息读到内存中,专门用来处理图片的。

BufferedImage.TYPE_3BYTE_BGR : 表示一个具有 8 位rgb颜色分量的图像,对应于 windows 风格的 bgr 颜色模型,具有用 3 字节存储的 blue、green 和red 三种颜色。不存在alpha。该图像具有一个 componentcolormodel。当具有透明 alpha 的数据存储在此类型的图像中时,必须将颜色数据调整为非预乘形式并丢弃 alpha,如 alphacomposite 文档中的描述。

 Font.PLAIN :Font是JAVA中的字体类,PLAIN是Font类中的静态常量( static final ) ,表示是:普通样式常量。其他可用样式为:BOLD :粗体样式常量 ,ITALIC: 斜体样式常量.如可以如下初始化对象:Font textFont = new Font("宋体" , Font.BOLD , 23);该字体表示23磅粗体的宋体字。

Graphics2D类(我也是搜的具体不太了解 大神可以解释解释):就是一个绘图类 Graphics2D继承自Graphics

bi.createGraphics():就是在bi对象绘图。

FontRenderContext 类是正确测量文本所需的信息容器。

 g.getFontRenderContext();获取FontRenderContext对象。

g.drawString(code, (int)x, (int)baseY);可能是生成图片的额位置。(我也是猜的)

Dispose和Close应该是一样的,(具体百度去)。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值