欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2017年05月05日 09:11:40   作者:PettyKoKo  
這篇文章主要為大家詳細介紹了簡單實現(xiàn)Java驗證碼功能的代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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


運行效果如下:

代碼如下:

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();//隨機對象 
 //設(shè)置字體范圍 
 private String[] fontNames = {"宋體","華文楷體","黑體","華文新魏","華文隸書","微軟雅黑","楷體"}; 
 //設(shè)置字體樣式范圍 
 private int[] fontstyle = {0,1,2,3}; 
 //設(shè)置字號范圍 
 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; 
 } 
 
 
 // 隨機返回字體顏色 
 private Color randomColor() { 
     int r = random.nextInt(256); 
     int g = random.nextInt(256); 
     int b = random.nextInt(256); 
   return new Color(r, g, b); 
   } 
 
 
 //隨機返回字體樣式 
 private Font randomFont(){ 
  //隨機生成字體下標,隨機從給定的范圍內(nèi)獲取一個字體 
  int index = random.nextInt(fontNames.length); 
  String name = fontNames[index]; 
 
  //隨機生成字體樣式下標,隨即從給定的范圍內(nèi)獲取一個字體樣式 
  index = random.nextInt(fontstyle.length); 
  int style = fontstyle[index]; 
 
  //隨機生成字體大小下標,隨機從給定的范圍內(nèi)獲取一個字體大小 
  index = random.nextInt(fontSizes.length); 
  int size = fontSizes[index]; 
 
  return new Font(name,style,size); 
 } 
 
 
 //隨機返回字體內(nèi)容 
 private String randomChar(){ 
  int index = random.nextInt(codes.length()); 
 
  return codes.charAt(index)+""; 
 } 
 
 
 //隨機返回幾條干擾線 
 private void getLine(BufferedImage img){ 
  //設(shè)置干擾線的寬度為1.5倍寬,隨機畫五條 
  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(){ 
  /** 
   * 隨機生成字符0-9A-Za-z,設(shè)置字體,字號,是否粗體,字符顏色,都是隨機的 
   */ 
  BufferedImage img = createImage(); 
 
  this.getLine(img); 
 
  //獲取畫筆 
  Graphics g = img.getGraphics(); 
 
  //畫內(nèi)容 
  for(int i=0;i<4;i++){ 
   g.setColor(this.randomColor());//獲取隨機顏色 
   g.setFont(this.randomFont());//獲取隨機字體 
   g.drawString(this.randomChar(),w/4*i,h-5);//獲取字符串隨機內(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>一次性驗證碼</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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java Lombok簡介、使用、工作原理、優(yōu)缺點

    Java Lombok簡介、使用、工作原理、優(yōu)缺點

    這篇文章主要介紹了Java Lombok簡介、使用、工作原理、優(yōu)缺點的相關(guān)資料,幫助大家更好的理解和學習使用Java Lombok,感興趣的朋友可以了解下
    2021-03-03
  • Java中二維數(shù)組的正確使用方法介紹

    Java中二維數(shù)組的正確使用方法介紹

    Java中有一維數(shù)組,二維數(shù)組以及多維數(shù)組,在這篇文章中,將給大家詳細介紹一下如何正確使用Java中的二維數(shù)組,感興趣的小伙伴跟著小編一起學習吧
    2023-05-05
  • Invalid?bound?statement?(not?found)出現(xiàn)原因以及解決辦法

    Invalid?bound?statement?(not?found)出現(xiàn)原因以及解決辦法

    這篇文章主要給大家介紹了關(guān)于Invalid?bound?statement?(not?found)出現(xiàn)原因以及解決辦法的相關(guān)資料,文中給出了詳細的解決方法,需要的朋友可以參考下
    2023-07-07
  • Java去重排序之Comparable與Comparator的使用及說明

    Java去重排序之Comparable與Comparator的使用及說明

    這篇文章主要介紹了Java去重排序之Comparable與Comparator的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Java?輸入輸出?IO?NIO?AIO三兄弟對比分析對比分析

    Java?輸入輸出?IO?NIO?AIO三兄弟對比分析對比分析

    這篇文章主要為大家介紹了Java?輸入輸出?IO?NIO?AIO三兄弟對比分析對比分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-04-04
  • SpringBoot學習之基于注解的緩存

    SpringBoot學習之基于注解的緩存

    spring boot對緩存支持非常靈活,我們可以使用默認的EhCache,也可以整合第三方的框架,只需配置即可,下面這篇文章主要給大家介紹了關(guān)于SpringBoot學習之基于注解緩存的相關(guān)資料,需要的朋友可以參考下
    2022-03-03
  • JAVA實現(xiàn)紅包分發(fā)的示例代碼

    JAVA實現(xiàn)紅包分發(fā)的示例代碼

    這篇文章主要介紹了JAVA實現(xiàn)紅包分發(fā)的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-03-03
  • maven查看依賴樹的方法實現(xiàn)

    maven查看依賴樹的方法實現(xiàn)

    Maven依賴樹是以當前項目的POM文件為根節(jié)點,列出了所有直接或間接依賴的依賴樹結(jié)構(gòu),本文就詳細的來介紹一下如何查看,感興趣的可以了解一下
    2023-08-08
  • java實現(xiàn)快速排序算法

    java實現(xiàn)快速排序算法

    快速排序算法是基于分治策略的另一個排序算法。其基本思想是:對輸入的子數(shù)組a[p:r],按以下三個步驟進行排序。 1) 分解(Divide)(2) 遞歸求解(Conquer) (3) 合并(Merge)
    2015-04-04
  • Spring?MVC異步上傳、跨服務(wù)器上傳和文件下載功能實現(xiàn)

    Spring?MVC異步上傳、跨服務(wù)器上傳和文件下載功能實現(xiàn)

    這篇文章主要介紹了Spring?MVC異步上傳、跨服務(wù)器上傳和文件下載功能實現(xiàn),本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-07-07

最新評論