java图片验证码实现(底层源码)

图片验证码的实现

方法
1) 将验证码文字存入session作用域
2) 表单提交时,对比session中的验证码和表单中的验证码

1.生成随机数方法

    //生成随机数
    private String makeNum() {
        Random random = new Random();
        String num = random.nextInt(9999999) + "";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 7 - num.length(); i++) {
            sb.append("0");
        }
        System.out.println("后台获取验证---" + num);
        num = sb.toString() + num;
        return num;

    }

2.总方法(图片显示)

package com.xyj.servlet;

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

/**
 * @author xyj
 * @date 2020/3/22 -17:45
 */
public class ImageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setHeader("refresh", "1");
        BufferedImage image = new BufferedImage(800, 300, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = (Graphics2D) image.getGraphics();
        g.setColor(Color.blue);
        g.fillRect(0, 0, 800, 400);
        //给图片写数据
        g.setColor(Color.BLACK);
        g.setFont(new Font(null, Font.BOLD, 200));
        g.drawString(makeNum(), 0, 200);

        resp.setContentType("image/jpeg");
        resp.setDateHeader("expires", -1);
        resp.setHeader("Cache-Control", "no-cache");
        resp.setHeader("Pragma", "no-cache");
        ImageIO.write(image, "jpg", resp.getOutputStream());

    }

    //生成随机数
    private String makeNum() {
        Random random = new Random();
        String num = random.nextInt(9999999) + "";
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < 7 - num.length(); i++) {
            sb.append("0");
        }
        System.out.println("后台获取验证---" + num);
        num = sb.toString() + num;
        return num;

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

}

3.web.xml

    <!--生成随机数-->
    <servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>com.xyj.servlet.ImageServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ImageServlet</servlet-name>
        <url-pattern>/img</url-pattern>
    </servlet-mapping>

4.图片下载

package com.xyj.servlet;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;

/**
 * @author xyj
 * @date 2020/3/23 -17:29
 */
public class FileServlet extends HelloServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //获取下载的路径
        String realPath = "D:\\IDEA SAVES\\javaweb\\javaweb-servlet\\src\\main\\resources\\1.jpg";
        System.out.println("下载的路径为" + realPath);
        String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);
        resp.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        FileInputStream in = new FileInputStream(realPath);
        int len = 0;
        byte[] buffer = new byte[1024];
        ServletOutputStream out = resp.getOutputStream();
        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }

}

    <!--下载文件-->
    <servlet>
        <servlet-name>filedown</servlet-name>
        <servlet-class>com.xyj.servlet.FileServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>filedown</servlet-name>
        <url-pattern>/down</url-pattern>
    </servlet-mapping>

你可能感兴趣的:(小功能)