Day03【Response】综合案例-图片验证码(新开发方式)

图片验证码效果

在这里插入图片描述
Servlet随机生成一个字符串,然后将字符串转成图片,通过响应的字节流写到浏览器的img标签

图片验证码分析

在这里插入图片描述

图片验证码的业务逻辑

test\java\com\wzx\service\TestVerificationCodeService.java

public class TestVerificationCodeService {
    @Test
    public void test01() throws IOException {
        //1:创建一个验证码的业务
        VerificationCodeService vcs = new VerificationCodeService();
        //2:生产一个随机的4个字符组成的字符串
        String code =  vcs.createRandomCode();
        System.out.println(code);
        //3:将字符串转成图片
        BufferedImage image =  vcs.changeStringToImage(code);
       //4:使用OutputStream写到浏览器
        System.out.println(image);
        File file = new File(System.currentTimeMillis()+".jpg");
        file.createNewFile();
        FileOutputStream outputStream = new FileOutputStream(file);
        ImageIO.write(image,"jpeg",outputStream);//参1,内存中的图片  参2,格式  参3,字节输出流
    }
}

随机字符串生成

src\com\wzx\service\VerificationCodeService.java

//生成验证码图片的业务类
public class VerificationCodeService {
    //生成验证码字符串
    public String createRandomCode() {
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";//36
        System.out.println(str.length());
        //Random类  产生指定范围内的随机数
        Random random = new Random();

        StringBuilder sb = new StringBuilder();

        //截取字符
        for (int i = 0; i < 4; i++) {
            int index = random.nextInt(str.length());//
            char letter = str.charAt(index);
            //System.out.println(letter);
            sb.append(letter);
        }

        return sb.toString();
    }

随机字符串转图片

代码不需要敲,只需要根据不同的样式要求,能修改字体大小,颜色即可

src\com\wzx\service\VerificationCodeService.java

  //字符串转图片
    public BufferedImage changeStringToImage(String code) {
        Random rd = new Random();
        //创建一个画布
        BufferedImage image = new BufferedImage(75, 28, BufferedImage.TYPE_INT_RGB);
        //画笔
        Graphics g = image.getGraphics();
        //给画笔设置颜色
        g.setColor(new Color(240,240,240));  //#00000   FFFFFF
        //设置验证码的 背景色
        g.fillRect(0, 0, 75, 28);

        g.setFont(new Font("宋体",Font.BOLD,16));

        g.setColor(new Color(0,0,0));  //#00000   FFFFFF
        // g.drawString(checkCodeStr, 20, 20);
        for (int i = 0; i <4 ; i++) {
            //画字符
            g.setColor(new Color(rd.nextInt(120),rd.nextInt(120),rd.nextInt(120)));
            g.drawString(code.charAt(i)+"", 16*i + rd.nextInt(16), 15 + rd.nextInt(10) );
            if(i % 2 == 0) {//画线
                g.setColor(new Color(rd.nextInt(120), rd.nextInt(120), rd.nextInt(120)));
                g.drawLine(rd.nextInt(75), rd.nextInt(28), rd.nextInt(75), rd.nextInt(28));
            }
        }
        return image;
    }
}

img标签显示验证码图片

web\login.html

<form method="post">
    username:<input type="text" name="username"/><br/>
    password:<input type="password" name="password"/><br/>
    code:<input type="text" name="code"/> <img id="img" src="/web01_reponse/img"/><br/>
    <input type="submit" value="login">
</form>

src\com\wzx\pack06_code\Demo06ImageCodeSevlet.java

@WebServlet("/img")
public class Demo06ImageCodeSevlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1:创建一个验证码的业务
        VerificationCodeService vcs = new VerificationCodeService();
        //2:生产一个随机的4个字符组成的字符串
        String code =  vcs.createRandomCode();
        System.out.println(code);
        //3:将字符串转成图片
        BufferedImage image =  vcs.changeStringToImage(code);
        //4:使用OutputStream写到浏览器
        System.out.println(image);
        ImageIO.write(image,"jpeg",response.getOutputStream());//参1,内存中的图片  参2,格式  参3,字节输出流
    }
}

img标签点击刷新

<script type="application/javascript">
    var img = document.getElementById("img");
    img.onclick = function(){
        img.src = "/web01_reponse/img?time="+new Date();
    }

</script>
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

翁老师的教学团队

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值