亲宝软件园·资讯

展开

Java制作验证码的完整实例代码

不会秃头的猿 人气:0

JAVA代码制作验证码,可用于注册等功能页面

要导入servlet-api.jar包

创建验证码的Util工具类:

先创建图片并生成随机的验证码字母

设置图片的底色,并用setFont函数将验证码画在图片上,如果想让验证码难一点,可以添加for循环的代码给图片增加旋转角度

给验证码增加干扰线,提高安全性

设置边框

创建VerifyPic的servlet,通过Util类获取代码,并存入session中,然后输送去前端页面

实打

前端页面验证码如下:

完整Util工具类代码如下:

package com.sport.util;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

public class VerifyUtil {
 public static Map<String, Object> generateVerify(){
  //创建一张图片
  BufferedImage verifyPic = new BufferedImage(120,40,BufferedImage.TYPE_3BYTE_BGR);
  //通过图片获取画笔
  Graphics2D g = verifyPic.createGraphics();
  //准备一个字母+数字的字典
  String letters = "23456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  //规定验证码的位数
  int verifyLength = 4;
  //生成随机验证码
  String verifyCode = "";
  //循环取值
  for(int i=0; i<verifyLength; i++) {
   verifyCode +=letters.charAt((int)(Math.random()*letters.length()));
  }
  //将图片的底板由黑变白
  g.setColor(Color.white);
  g.fillRect(0, 0, 120, 40);
  //将验证码画在图片之上
  g.setFont(new Font("微软雅黑",Font.BOLD, 24));
  for (int i = 0; i < verifyLength; i++) {
   //随机产生一个角度
   double theta = Math.random() * Math.PI / 4 * ((int)(Math.random()*2) == 0?1:-1);
   //产生偏转
   g.rotate(theta, 24+i*22, 20);
   //每画一个字幕之前都随机给一个颜色
   g.setColor(new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256)));
   g.drawString(verifyCode.charAt(i)+"", 20+i*22 , 26);
   //回正
   g.rotate(-theta, 24+i*22, 20);
  }
  //加入干扰线
  for (int i = 0; i < 5; i++) {
   //给随机颜色
   g.setColor(new Color((int)(Math.random()*256), (int)(Math.random()*256), (int)(Math.random()*256)));
   //画线
   g.drawLine((int)(Math.random()*120), (int)(Math.random()*40),
     (int)(Math.random()*120), (int)(Math.random()*40));
  }
  //设置边框颜色
  g.setColor(Color.black);
  //给验证码一个外边框
  g.drawRect(0, 0, 118, 38);

  //将验证码和图片一起存入map
  Map<String, Object> data = new HashMap<String, Object>();
  data.put("verifyCode", verifyCode);
  data.put("verifyPic", verifyPic);

  return data;
 }
}

完整VerifyPic的代码如下:

package com.sport.servlet;

import com.sport.util.VerifyUtil;

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.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

@WebServlet("/VerifyPic")
public class VerifyPic extends HttpServlet {
 protected void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  Map<String, Object> data = VerifyUtil.generateVerify();
  //获取验证码
  String verifyCode = (String) data.get("verifyCode");
  //将验证码存入session
  HttpSession session = request.getSession();
  session.setAttribute("verifyCode", verifyCode);
  //获取图片
  BufferedImage verifyPic = (BufferedImage) data.get("verifyPic");
  //输出图片给前端,使用工具类ImageIO
  ImageIO.write(verifyPic, "jpg", response.getOutputStream());

 }

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

总结

加载全部内容

相关教程
猜你喜欢
用户评论