1. <ul id="0c1fb"></ul>

      <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
      <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区

      RELATEED CONSULTING
      相關(guān)咨詢
      選擇下列產(chǎn)品馬上在線溝通
      服務(wù)時(shí)間:8:30-17:00
      你可能遇到了下面的問(wèn)題
      關(guān)閉右側(cè)工具欄

      新聞中心

      這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
      java如何實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼功能

      這篇文章主要講解了java如何實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼功能,內(nèi)容清晰明了,對(duì)此有興趣的小伙伴可以學(xué)習(xí)一下,相信大家閱讀完之后會(huì)有幫助。

      創(chuàng)新互聯(lián)是專業(yè)的臨川網(wǎng)站建設(shè)公司,臨川接單;提供做網(wǎng)站、網(wǎng)站建設(shè),網(wǎng)頁(yè)設(shè)計(jì),網(wǎng)站設(shè)計(jì),建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進(jìn)行臨川網(wǎng)站開(kāi)發(fā)網(wǎng)頁(yè)制作和功能擴(kuò)展;專業(yè)做搜索引擎喜愛(ài)的網(wǎng)站,專業(yè)的做網(wǎng)站團(tuán)隊(duì),希望更多企業(yè)前來(lái)合作!

      最近要做一個(gè)網(wǎng)站,要求實(shí)現(xiàn)驗(yàn)證碼程序,經(jīng)過(guò)不斷調(diào)試,終于成功實(shí)現(xiàn)功能。

      一、驗(yàn)證碼生成類(lèi)

      生成驗(yàn)證碼的話需要用到j(luò)ava的Graphics類(lèi)庫(kù),畫(huà)出一個(gè)驗(yàn)證碼
      廢話不多說(shuō),直接上代碼

      package verificationCode;
      
      
      import java.awt.Color;
      import java.awt.Font;
      import java.awt.Graphics;
      import java.awt.image.BufferedImage;
      import java.awt.image.RenderedImage;
      import java.io.File;
      import java.io.FileNotFoundException;
      import java.io.FileOutputStream;
      import java.io.IOException;
      import java.io.OutputStream;
      import java.util.HashMap;
      import java.util.Map;
      import java.util.Random;
      
      import javax.imageio.ImageIO;
      
      public class generateCode {
       private static int width = 150;// 定義圖片的width
       private static int height = 48;// 定義圖片的height
       private static int codeCount = 4;// 定義圖片上顯示驗(yàn)證碼的個(gè)數(shù)
       private static int xx = 25;
       private static int fontHeight = 42;
       private static int codeY = 42;
       private static char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
         'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };
      
       /**
        * 生成一個(gè)map集合
        * code為生成的驗(yàn)證碼
        * codePic為生成的驗(yàn)證碼BufferedImage對(duì)象
        * @return
        */
       public static Map generateCodeAndPic() {
        // 定義圖像buffer
        BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // Graphics2D gd = buffImg.createGraphics();
        // Graphics2D gd = (Graphics2D) buffImg.getGraphics();
        Graphics gd = buffImg.getGraphics();
        // 創(chuàng)建一個(gè)隨機(jī)數(shù)生成器類(lèi)
        Random random = new Random();
        // 將圖像填充為白色
        gd.setColor(Color.WHITE);
        gd.fillRect(0, 0, width, height);
      
        // 創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來(lái)定。
        Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
        // 設(shè)置字體。
        gd.setFont(font);
      
        // 畫(huà)邊框。
        gd.setColor(Color.BLACK);
        gd.drawRect(0, 0, width - 1, height - 1);
        gd.setFont(font);
        // 隨機(jī)產(chǎn)生40條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到。
        int red = 0, green = 0, blue = 0;
      
      
        // randomCode用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼,以便用戶登錄后進(jìn)行驗(yàn)證。
        StringBuffer randomCode = new StringBuffer();
      
      
        // 隨機(jī)產(chǎn)生codeCount數(shù)字的驗(yàn)證碼。
        for (int i = 0; i < codeCount; i++) {
         // 得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字。
         String code = String.valueOf(codeSequence[random.nextInt(36)]);
         // 產(chǎn)生隨機(jī)的顏色分量來(lái)構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同。
         red = random.nextInt(255);
         green = random.nextInt(255);
         blue = random.nextInt(255);
         // 用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中。
         gd.setColor(new Color(red, green, blue));
         gd.drawString(code, (i + 1) * xx, codeY);
      
         // 將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。
         randomCode.append(code);
        }
        for (int i = 0; i < 60; i++) {
         red = random.nextInt(255);
         green = random.nextInt(255);
         blue = random.nextInt(255);
         // 用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中。
         gd.setColor(new Color(red, green, blue));
         int x = random.nextInt(width);
         int y = random.nextInt(height);
         int xl = random.nextInt(50);
         int yl = random.nextInt(50);
         gd.drawLine(x, y, x + xl, y + yl);
        }
        Map map =new HashMap();
        //存放驗(yàn)證碼
        map.put("code", randomCode);
        //存放生成的驗(yàn)證碼BufferedImage對(duì)象
        map.put("codePic", buffImg);
        return map;
       }
      
       public static void main(String[] args) throws Exception {
        //創(chuàng)建文件輸出流對(duì)象
        File file = new File("WebRoot/image/"+System.currentTimeMillis()+".jpg");
        FileOutputStream out = null;
        try {
         if (!file.exists()) {
          // 先得到文件的上級(jí)目錄,并創(chuàng)建上級(jí)目錄,在創(chuàng)建文件
          file.getParentFile().mkdir();
          file.createNewFile();
         }
         out = new FileOutputStream(file);
        Map map = generateCode.generateCodeAndPic();
        ImageIO.write((RenderedImage) map.get("codePic"), "jpeg", out);
        System.out.println("驗(yàn)證碼的值為:"+map.get("code"));
        } catch (FileNotFoundException e) {
         e.printStackTrace();
        } catch (IOException e) {
         e.printStackTrace();
        }
       }
      }

      二、驗(yàn)證碼驗(yàn)證類(lèi)

      package verificationCode;
      
      
      import java.io.IOException;
      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;
      
      
      @WebServlet("/checkCode")
      public class checkCode extends HttpServlet {
       private static final long serialVersionUID = 1L;
      
       protected void doGet(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
      
       }
      
       protected void doPost(HttpServletRequest request, HttpServletResponse response)
         throws ServletException, IOException {
        String code = request.getParameter("code");
        response.setCharacterEncoding("utf-8");//保證格式正確
        response.setContentType("text/html");
        // 驗(yàn)證驗(yàn)證碼
        String sessionCode = request.getSession().getAttribute("code").toString();
        if (code != null && !"".equals(code) && sessionCode != null && !"".equals(sessionCode)) {
         if (code.equalsIgnoreCase(sessionCode)) {
          response.getWriter().println("驗(yàn)證通過(guò)!");
         } else {
          response.getWriter().println("驗(yàn)證失敗!");
         }
        } else {
         response.getWriter().println("驗(yàn)證失敗!");
        }
       }
      
      }

      三、驗(yàn)證碼傳輸類(lèi)

      用到doget方法

      package verificationCode;
      
      import java.awt.Color;
      import java.awt.Font;
      import java.awt.Graphics;
      import java.awt.image.BufferedImage;
      import java.awt.image.RenderedImage;
      import java.io.IOException;
      import java.util.HashMap;
      import java.util.Map;
      import java.util.Random;
      
      import javax.imageio.ImageIO;
      import javax.servlet.ServletException;
      import javax.servlet.ServletOutputStream;
      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 verificationCode.generateCode;
      /**
       * Servlet implementation class CodeServlet
       */
      @WebServlet("/getCode")
      public class CodeServlet extends HttpServlet {
       private static final long serialVersionUID = 1L;
      
       protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 調(diào)用工具類(lèi)生成的驗(yàn)證碼和驗(yàn)證碼圖片
        Map codeMap = generateCode.generateCodeAndPic();
      
        // 將四位數(shù)字的驗(yàn)證碼保存到Session中。
        HttpSession session = req.getSession();
        session.setAttribute("code", codeMap.get("code").toString());
      
        // 禁止圖像緩存。
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 0);
      
        resp.setContentType("image/jpeg");
      
        // 將圖像輸出到Servlet輸出流中。
        ServletOutputStream sos;
        try {
         sos = resp.getOutputStream();
         ImageIO.write((RenderedImage) codeMap.get("codePic"), "jpeg", sos);
         sos.close();
        } catch (IOException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        }
      
       }
      
      }

      四、jsp驗(yàn)證demo

      <%@ page language="java" contentType="text/html; charset=UTF-8"
       pageEncoding="UTF-8"%>
      
      
      
      
      驗(yàn)證碼頁(yè)面
      
      
      
       
      請(qǐng)輸入驗(yàn)證碼:
      驗(yàn)證碼

      總結(jié)

      雖然只是一個(gè)小小的demo,但是實(shí)際上用到了許多知識(shí),比如生成驗(yàn)證碼的畫(huà)圖,傳輸驗(yàn)證碼doget,驗(yàn)證驗(yàn)證碼的dopost和ajax動(dòng)態(tài)更換驗(yàn)證碼,麻雀雖小五臟俱全啊。

      看完上述內(nèi)容,是不是對(duì)java如何實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼功能有進(jìn)一步的了解,如果還想學(xué)習(xí)更多內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。


      當(dāng)前名稱:java如何實(shí)現(xiàn)簡(jiǎn)單的驗(yàn)證碼功能
      網(wǎng)址分享:http://www.ef60e0e.cn/article/giehpd.html
      99热在线精品一区二区三区_国产伦精品一区二区三区女破破_亚洲一区二区三区无码_精品国产欧美日韩另类一区
      1. <ul id="0c1fb"></ul>

        <noscript id="0c1fb"><video id="0c1fb"></video></noscript>
        <noscript id="0c1fb"><listing id="0c1fb"><thead id="0c1fb"></thead></listing></noscript>

        镇远县| 游戏| 河南省| 台东市| 佛冈县| 瑞安市| 朔州市| 金溪县| 水富县| 西峡县| 库尔勒市| 新兴县| 福贡县| 界首市| 永定县| 册亨县| 乐亭县| 且末县| 长武县| 益阳市| 泉州市| 自治县| 惠东县| 梓潼县| 杭州市| 西藏| 牟定县| 旅游| 务川| 蒙城县| 邓州市| 阿城市| 凭祥市| 焉耆| 鄂州市| 邵阳市| 鸡西市| 饶平县| 瑞金市| 定西市| 伊川县|