java實現驗證碼功能

來源:互聯網
上載者:User

標籤:

package com.verification_code.servlet;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class ImgServlet extends HttpServlet{    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        // TODO Auto-generated method stub        doPost(req,resp);    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {                // TODO Auto-generated method stub        //設定響應開頭        resp.setContentType("image/jpeg");        //設定頁面不緩衝        resp.setHeader("pragma", "no-cache");        resp.setHeader("cache-control", "no-cache");        resp.setDateHeader("expires", 0);        OutputStream out = resp.getOutputStream();        //設定圖片的寬高        int width=88,height=20;        BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);        Graphics g = image.getGraphics();        //產生隨機類        Random random = new Random();        g.setColor(Color.WHITE);        g.fillRect(0, 0, width, height);        //設定字型        g.setFont(new Font("Times New Roman",Font.BOLD+Font.ITALIC,18));        // 畫邊框        g.setColor(getRandColor(0, 20)); // ---2        g.drawRect(0, 0, width - 1, height - 1);        // 隨機產生幹擾線        int count = 100;        int lineWidth = 3; // 幹擾線的寬度        for (int i = 0; i < count; i++) {            g.setColor(getRandColor(150, 200)); // ---3            int x = random.nextInt(width - lineWidth - 1) + 1; // 保證畫在邊框之內            int y = random.nextInt(height - lineWidth - 1) + 1;            int xl = random.nextInt(lineWidth);            int yl = random.nextInt(lineWidth);            g.drawLine(x, y, x + xl, y + yl);        }        //產生隨機驗證碼        String sRand="";        String str[]={"0","1","2","3","4","5","6","7","8","9",                "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"};        for(int i=0;i<4;i++){            int rand = random.nextInt(36);            sRand+=str[rand];            //設定隨機顏色            g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));            //繪製            g.drawString(str[rand], 20*i+8, 16);        }        req.getSession().setAttribute("Verification_code",sRand);        g.dispose();        ImageIO.write(image, "JPEG", out);    }    public Color getRandColor(int fc, int bc) { // 取得給定範圍隨機顏色        Random random = new Random();        if (fc > 255) {            fc = 255;        }        if (bc > 255) {            bc = 255;        }        int r = fc + random.nextInt(bc - fc);        int g = fc + random.nextInt(bc - fc);        int b = fc + random.nextInt(bc - fc);        return new Color(r, g, b);    }}

頁面

<img id="id_code" name="code" src="image" title="看不清,換一張" onclick="refresh()" style="display: inline;"><script type="text/javascript">    function refresh(){        document.getElementById("id_code").src="image?"+Math.random();        //loginForm.code.src="image?"+Math.random();    }
</script>

 

java實現驗證碼功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.