在servlet上实现 生成验证码图片 功能

该文章展示了一个JavaServlet实现的生成验证码图片的功能。代码创建了一个BufferedImage对象,设置了字符集并从中随机选择字符绘制到图片上,同时添加了干扰线。生成的验证码存储在HttpSession中,并以JPEG格式写入HTTP响应的输出流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在servlet上实现 生成验证码图片 功能

代码:

package com.dong.servlet;

/**
 * 生成验证码图片
 */

import javax.imageio.ImageIO;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet("/CodeServlet")
public class CodeServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public CodeServlet() {
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        BufferedImage bufferedImage=new BufferedImage(80,25,BufferedImage.TYPE_INT_RGB);
        Graphics graphics=bufferedImage.getGraphics();
        graphics.fillRect(0,0,80,25);

        //验证码字符范围
        char[] ch="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
        Random random=new Random();
        int index;
        StringBuffer stringBuffer=new StringBuffer();// 保存字符串
        for(int i=0;i<4;i++){
            index=random.nextInt(ch.length);
            graphics.setColor(new Color(random.nextInt(225),random.nextInt(225),random.nextInt(225)));
            Font font=new Font("宋体",30,20);
            graphics.setFont(font);
            graphics.drawString(ch[index]+"",(i*20)+2,23);
            stringBuffer.append(ch[index]);
        }
        // 设置验证码中的干扰线
        for (int i=0;i<6;i++){
            // 随机获取干扰线的起点和终点
            int xStart=(int) (Math.random()*80);
            int yStart=(int) (Math.random()*25);
            int xEnd=(int) (Math.random()*80);
            int yEnd=(int) (Math.random()*25);
            graphics.getColor();
            graphics.drawLine(xStart,yStart,xEnd,yEnd);
        }
        HttpSession httpSession=request.getSession();// 保存到session
        httpSession.setAttribute("code",stringBuffer.toString());
        ImageIO.write(bufferedImage,"JPG",response.getOutputStream());// 写到输出流
    }
    private static Color interLine(int Low, int High){
        if (Low>225)
            Low=225;
        if (High > 255)
            High = 255;
        if (Low < 0)
            Low = 0;
        if (High < 0)
            High = 0;
        int interval=High-Low;
        int r=Low+(int) (Math.random()*interval);
        int g=Low+(int) (Math.random()*interval);
        int b=Low+(int) (Math.random()*interval);
        return new Color(r,g,b);
    }
}

在浏览器上运行结果:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

captain_dong

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

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

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

打赏作者

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

抵扣说明:

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

余额充值