验证码

package com.zit.sevlet;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/check")
public class SecurityCode extends HttpServlet {
    private int codelength = 4;
    private String securitycodes;
    private String codes = "abcdefghijkmnpqrstuvwxyz23456789";
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("image/jpeg");
        StringBuilder sb = null;
        try {
            int width = 400;
            int heigth = 200;
            int linecounts = 50;
            int codesnums = 20;
            int circlenums = 5;
            //设置画布
            BufferedImage bi = new BufferedImage(width, heigth, BufferedImage.TYPE_INT_RGB);
            Graphics g = bi.getGraphics();
            g.setColor(Color.white);
            g.fillRect(1, 1, width - 2, heigth - 2);
            //获取随机验证码
            Random r = new Random();
            sb = new StringBuilder();
            for (int i = 1; i <= codelength; i++) {
                g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255)));
                g.setFont(new Font("微软雅黑", 0, r.nextInt(50) + 80));
                char c = codes.charAt(r.nextInt(codes.length()));
                sb.append(c);
                g.drawString(c + "", 80 * i, r.nextInt(50) + 100);
            }

            HttpSession session=req.getSession();
            session.setAttribute("codes",sb.toString());

            //设置干扰线
            for (int i = 1; i <= linecounts; i++) {
                g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255), r.nextInt(100) + 50));
                g.drawLine(r.nextInt(width), r.nextInt(width), r.nextInt(width), r.nextInt(width));
            }
            // 设置干扰字母
            for (int i = 0; i < codesnums; i++) {
                g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255), r.nextInt(80) + 30));
                g.setFont(new Font("微软雅黑", 0, r.nextInt(50) + 30));
                char d = codes.charAt(r.nextInt(codes.length()));
                g.drawString(d + "", 20 * (i + 1) + r.nextInt(120), r.nextInt(100) + 70);
            }
            //设置干扰圆
            for (int i = 0; i < circlenums; i++) {
                g.setColor(new Color(r.nextInt(255), r.nextInt(255), r.nextInt(255), r.nextInt(50) + 10));
                int w = r.nextInt(50) + 30;
                g.fillOval(60 * (i + 1), r.nextInt(100) + 50, w, w);
            }

            ImageIO.write(bi,"jpg",resp.getOutputStream());


        } catch (Exception e) {
            e.printStackTrace();
        }

    }



    public String getSecuritycodes() {
        return securitycodes = getCodes();
    }

    public void setSecuritycodes(String securitycodes) {
        this.securitycodes = securitycodes;
    }

    public int getCodelength() {
        return codelength;
    }

    public void setCodelength(int codelength) {
        this.codelength = codelength;
    }

    public String getCodes() {
        return codes;
    }

    public void setCodes(String codes) {
        this.codes = this.codes;
    }
}

jsp 页面获取验证码图片

<img src="/check" width="90" height="38" onclick="this.src='/check?'+new Date()" class="ai"/>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值