Java制作验证码的完整代码
生活随笔
收集整理的這篇文章主要介紹了
Java制作验证码的完整代码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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);//將驗證碼和圖片一起存入mapMap<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");//將驗證碼存入sessionHttpSession session = request.getSession();session.setAttribute("verifyCode", verifyCode);//獲取圖片BufferedImage verifyPic = (BufferedImage) data.get("verifyPic");//輸出圖片給前端,使用工具類ImageIOImageIO.write(verifyPic, "jpg", response.getOutputStream());}protected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {doGet(request, response);} }總結
以上是生活随笔為你收集整理的Java制作验证码的完整代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跟我师兄聊天引发的思考
- 下一篇: Linux系统编程 / triggerh