간단 한 인증 코드 생 성 servlet

3745 단어 자바
package com.day10.pm.servlet;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class VerifyCodeServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //         。
        int width = 70;
        //         。
        int height = 30;
        BufferedImage buffImg = new BufferedImage(width, height,
                BufferedImage.TYPE_INT_RGB);
        Graphics2D g = buffImg.createGraphics();

        //            。
        Random random = new Random();

        //        (      ,    )
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        //     ,                。
        Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28);
        //     。
        g.setFont(font);

        //    。
        g.setColor(Color.BLACK);
        g.drawRect(0, 0, width - 1, height - 1);
        //     155    ,                  。
//g.setColor(Color.GRAY);
        g.setColor(getRandColor(160,200));
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int xl = random.nextInt(12);
            int yl = random.nextInt(12);
            g.drawLine(x, y, x + xl, y + yl);
        }

        // randomCode            ,           。
        StringBuffer randomCode = new StringBuffer();

        //       4    
        int length = 4;
        //        :  "a-z"   "0-9"
        String base = "abcdefghjkmnprstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ2345678";

        int size = base.length();

        //     4       。
        for (int i = 0; i < length; i++) {
            //             。
            int start = random.nextInt(size);
            String strRand = base.substring(start, start + 1);

            //                   。
//       (      ,    )
//g.setColor(getRandColor(1, 100));
            
//           ,          ,        
            g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));

            g.drawString(strRand, 15 * i + 6, 24);

            //               。
            randomCode.append(strRand);
        }
        //             Session 。
        HttpSession session = request.getSession();
        session.setAttribute("verifyCode_session", randomCode.toString());

        //    
        g.dispose();

        //       。
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
       response.setDateHeader("Expires", 0);

       response.setContentType("image/jpeg");

       //       Servlet    。
       ServletOutputStream sos = response.getOutputStream();
       ImageIO.write(buffImg, "jpeg", sos);
       sos.flush();
       sos.close();

   }

   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);
   }

}

좋은 웹페이지 즐겨찾기