Javaweb中快速生成验证码Captcha

生成验证码的两种方式:

1.自己手动绘画一个

2.使用插件ValidateCode.jar

1.自己手动绘画一个验证码

代码演示:

package com.tcb.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ServletByCaptcha extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ServletByCaptcha() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置图片的宽高
        int width=120;
        int height=50;

        //2.创建图像流对象(画布),以及获得画笔
        BufferedImage image=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics graphics=image.getGraphics();

        //3.设置画笔颜色和填充画布(即设置画布背景)
        graphics.setColor(Color.RED);
        graphics.fillRect(0, 0, width, height);

        //4.写字前,设置画笔输出的字体    字体类型         粗细/倾斜等       字体大小
        Font font =new Font("微软雅黑", Font.BOLD, 30);
        graphics.setFont(font);

        //5.在画布上写字--即生成随机码
        StringBuffer buffer=new StringBuffer();
        Random random=new Random();

        //6.循环填写随机码
        for(int i=0;i<4;i++){
            graphics.setColor(Color.GREEN);
            int num=random.nextInt(10);
            //添加随机码
            graphics.drawString(""+num, i*30+10, 40);
            buffer.append(num);

            //添加背景斜线
            graphics.setColor(Color.GRAY);
            graphics.drawLine(random.nextInt(width), random.nextInt(height), random.nextInt(width), random.nextInt(height));
        }

        //7.将随机码存起了备用           
        HttpSession httpSession=request.getSession();
        httpSession.setAttribute("code", buffer);

        //8.输入验证码图片
        ImageIO.write(image, "jpg", response.getOutputStream());

    }

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

}

运行效果图:

Javaweb中快速生成验证码Captcha_第1张图片

Javaweb中快速生成验证码Captcha_第2张图片


2.使用插件生成验证码

1)实现需要下载一个ValidateCode.jar的jar包。

下载地址:http://download.csdn.net/detail/baidu_37107022/9852253

将下载的jar包,放在当前工程的

WebContent/WEB-INF/lib 的文件名夹下。

如图:
Javaweb中快速生成验证码Captcha_第3张图片

2)演示代码如下:

package com.tcb.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.dsna.util.images.ValidateCode;

public class ServletByCaptcha2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public ServletByCaptcha2() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.设置验证码图片的宽高
        int width=120;
        int height=50;
        //2.生成验证码     参数1和2:宽和高       参数3:验证码的位数(一般是4位,比如7RV4,G86T等等)      参数4:干扰线的条数
        ValidateCode code=new ValidateCode(width, height, 4, 10);
        //3.输出验证码图片
        code.write(response.getOutputStream());

        //4.将随机码存起了备用 
        String codeString=code.getCode();
        System.out.println("====codeString======"+codeString);
    }

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

}

运行效果图:

Javaweb中快速生成验证码Captcha_第4张图片

Javaweb中快速生成验证码Captcha_第5张图片

你可能感兴趣的:(Javaweb)