溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

簡單實(shí)現(xiàn)Java驗(yàn)證碼功能

發(fā)布時(shí)間:2020-09-17 03:27:04 來源:腳本之家 閱讀:133 作者:PettyKoKo 欄目:編程語言

今晚看到網(wǎng)上有關(guān)驗(yàn)證碼的實(shí)現(xiàn)的代碼,很早就想寫一個(gè)了,感覺驗(yàn)證碼挺有意思的,于是就寫了一個(gè),然而后來一直加載不出圖片。嘗試了很多方法,后來終于解決了,原來是我項(xiàng)目里面的 web.xml中沒有部署servlet的映射,web.xml如下圖:

簡單實(shí)現(xiàn)Java驗(yàn)證碼功能

運(yùn)行效果如下:

簡單實(shí)現(xiàn)Java驗(yàn)證碼功能

代碼如下:

package model; 
 
import javax.imageio.ImageIO; 
import java.awt.*; 
import java.awt.image.BufferedImage; 
import java.io.IOException; 
import java.io.OutputStream; 
import java.util.Random; 
 
/** 
 * Created by Petty on 2017/5/4. 
 */ 
public class VCode { 
 
 private int w; //圖片寬 
 private int h;//圖片高 
 private Color bgColor = new Color(240,240,240);//背景色 
 private Random random = new Random();//隨機(jī)對象 
 //設(shè)置字體范圍 
 private String[] fontNames = {"宋體","華文楷體","黑體","華文新魏","華文隸書","微軟雅黑","楷體"}; 
 //設(shè)置字體樣式范圍 
 private int[] fontstyle = {0,1,2,3}; 
 //設(shè)置字號(hào)范圍 
 private int[] fontSizes = {24,25,26,27,28}; 
 //設(shè)置所有字符串范圍 
 private String codes="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; 
 
 
 //無參數(shù)構(gòu)造函數(shù) 
 public VCode(){ 
 
 } 
 
 //帶寬和高的構(gòu)造函數(shù) 
 public VCode(int w,int h){ 
  this.w = w; 
  this.h = h; 
 } 
 
 //返回一張背景圖片 
 private BufferedImage createImage(){ 
  /** 
   * 1.創(chuàng)建圖片 2.設(shè)置背景色 
   */ 
  //創(chuàng)建圖片 
  BufferedImage img = new BufferedImage(w,h,BufferedImage.TYPE_INT_RGB); 
  //設(shè)置背景色 
  Graphics g = img.getGraphics(); 
  g.setColor(bgColor); 
  g.fillRect(0,0,w,h); 
  return img; 
 } 
 
 
 // 隨機(jī)返回字體顏色 
 private Color randomColor() { 
     int r = random.nextInt(256); 
     int g = random.nextInt(256); 
     int b = random.nextInt(256); 
   return new Color(r, g, b); 
   } 
 
 
 //隨機(jī)返回字體樣式 
 private Font randomFont(){ 
  //隨機(jī)生成字體下標(biāo),隨機(jī)從給定的范圍內(nèi)獲取一個(gè)字體 
  int index = random.nextInt(fontNames.length); 
  String name = fontNames[index]; 
 
  //隨機(jī)生成字體樣式下標(biāo),隨即從給定的范圍內(nèi)獲取一個(gè)字體樣式 
  index = random.nextInt(fontstyle.length); 
  int style = fontstyle[index]; 
 
  //隨機(jī)生成字體大小下標(biāo),隨機(jī)從給定的范圍內(nèi)獲取一個(gè)字體大小 
  index = random.nextInt(fontSizes.length); 
  int size = fontSizes[index]; 
 
  return new Font(name,style,size); 
 } 
 
 
 //隨機(jī)返回字體內(nèi)容 
 private String randomChar(){ 
  int index = random.nextInt(codes.length()); 
 
  return codes.charAt(index)+""; 
 } 
 
 
 //隨機(jī)返回幾條干擾線 
 private void getLine(BufferedImage img){ 
  //設(shè)置干擾線的寬度為1.5倍寬,隨機(jī)畫五條 
  Graphics2D g =(Graphics2D) img.getGraphics(); 
  g.setColor(Color.BLACK); 
  g.setStroke(new BasicStroke(1.5f)); 
  for(int i=0;i<5;i++){ 
   int x1 = random.nextInt(w); 
   int y1 = random.nextInt(h); 
   int x2 = random.nextInt(w); 
   int y2 = random.nextInt(h); 
   g.drawLine(x1,y1,x2,y2); 
  } 
 } 
 
 
 //用戶調(diào)用該方法獲取圖片 
 public BufferedImage getImage(){ 
  /** 
   * 隨機(jī)生成字符0-9A-Za-z,設(shè)置字體,字號(hào),是否粗體,字符顏色,都是隨機(jī)的 
   */ 
  BufferedImage img = createImage(); 
 
  this.getLine(img); 
 
  //獲取畫筆 
  Graphics g = img.getGraphics(); 
 
  //畫內(nèi)容 
  for(int i=0;i<4;i++){ 
   g.setColor(this.randomColor());//獲取隨機(jī)顏色 
   g.setFont(this.randomFont());//獲取隨機(jī)字體 
   g.drawString(this.randomChar(),w/4*i,h-5);//獲取字符串隨機(jī)內(nèi)容 
  } 
  return img; 
 } 
 
 
 //用戶調(diào)用該方法保存圖片到本地 
 public void saveImage(BufferedImage img, OutputStream ous){ 
 
  try { 
   ImageIO.write(img,"JPEG",ous); 
  } catch (IOException e) { 
   e.printStackTrace(); 
  } 
 } 
}

package model; 
 
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 java.awt.image.BufferedImage; 
import java.io.IOException; 
 
/** 
 * Created by Petty on 2017/5/4. 
 */ 
@WebServlet(name = "BServlet") 
public class BServlet extends HttpServlet { 
 public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  VCode v = new VCode(70,35); 
  BufferedImage img = v.getImage(); 
  v.saveImage(img,response.getOutputStream()); 
 } 
 
 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
  VCode v = new VCode(70,35 ); 
  BufferedImage img = v.getImage(); 
  v.saveImage(img,response.getOutputStream()); 
 } 
} 

<%-- 
 Created by IntelliJ IDEA. 
 User: Petty 
 Date: 2017/5/4 
 Time: 22:28 
 To change this template use File | Settings | File Templates. 
--%> 
<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<html> 
 <head> 
 <title>一次性驗(yàn)證碼</title> 
 </head> 
 <body> 
 <form action="" method="get"> 
  <table align="center"> 
   <tr> 
    <td><img id="img" alt="" src="servlet/BServlet" onclick="changeNext()"></td> 
   </tr> 
  </table> 
 </form> 
 </body> 
</html> 
<script type="text/javascript"> 
 function changeNext(){ 
  var a=document.getElementById("img"); 
  a.src="servlet/BServlet?a="+new Date().getTime(); 
 } 
</script>

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI