zoukankan      html  css  js  c++  java
  • java实现随机验证码的图片

    链接地址:http://blog.sina.com.cn/s/blog_407a68fc010006qo.html

    1、一共需要2个常用java文件(RandomCode.java和RandomCodeCtrl.java):
     
    (a、)RandomCode.java是个普通的java文件;内容如下:
     
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import javax.imageio.ImageIO;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
     public class RandomCode {
       /**
        * 随机取得一个字体
        * @param Random random  随机数
        * @return Font 返回一个新字体
        */ 
      private Font getsFont(Random random){
       return new Font("Fixedsys",Font.CENTER_BASELINE,18);
      }
       /**
        * 返回一个随机颜色
        * @param int fc  随机数
        * @param int bc  随机数
        * @param Random random  随机数
        * @return Color 返回一个新颜色
        */ 
      private Color getRandColor(int fc,int bc,Random random){
            if(fc>255) fc=255;
            if(bc>255) bc=255;
            int r=fc+random.nextInt(bc-fc-6);
            int g=fc+random.nextInt(bc-fc-4);
            int b=fc+random.nextInt(bc-fc-8);
            return new Color(r,g,b);
        }
       /**
        * 生成随机数图片
        */ 
      public void getRandcode(HttpServletRequest request,HttpServletResponse response)throws Exception{
       System.setProperty("java.awt.headless","true");
       HttpSession session = request.getSession();
       int width=80, height=22;//设置图片大小
       BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
       Graphics g = image.getGraphics();
       Random random = new Random();
       g.fillRect(1, 1, width, height);//设定边框
       g.setFont(new Font("Times New Roman",Font.ROMAN_BASELINE,18));
       g.setColor(getRandColor(111,133,random));
       //产生随机线
       for (int i=0;i<11;i++){
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(13);
        int yl = random.nextInt(15);
        g.drawLine(x,y,x+xl,y+yl);
       }
       //产生随机点
       g.setColor(getRandColor(130,150,random));
       //产生5个随机数
       String sRand="";
       for (int i=0;i<5;i++){
           g.setFont(getsFont(random));
           g.setColor(new Color(random.nextInt(101),random.nextInt(111),random.nextInt(121)));
           String rand=String.valueOf(getRandomString(random.nextInt(36)));
           sRand+=rand;
           g.translate(random.nextInt(3),random.nextInt(3));
           g.drawString(rand,13*i,16);
       }
       session.removeAttribute("Rand");
       session.setAttribute("Rand",sRand);
       g.dispose();
       ImageIO.write(image, "JPEG", response.getOutputStream());
      }
      
      private String getRandomString(int num){
       String randstring = "0123456788ABCDEFGHIJKLMNOPQRSTUVWXYZ";
       return String.valueOf(randstring.charAt(num));
      }
     
     }
     
    (b、) RandomCodeCtrl.java是个servlet的java文件;内容如下:
     
    import java.io.IOException;
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
     
    public class RandomCodeCtrl extends HttpServlet {
     private static final long serialVersionUID = 1L;
     protected void doGet(HttpServletRequest req,
          HttpServletResponse resp) throws ServletException, IOException {
      resp.setContentType("image/jpeg");
      resp.setHeader("Pragma","No-cache");
      resp.setHeader("Cache-Control","no-cache");
      resp.setDateHeader("Expires", 0);
      RandomCode rc = new RandomCode();
      try{
       rc.getRandcode(req,resp);
      }catch(Exception e){
       System.err.println(e);
      }
     }
     public void doPost(HttpServletRequest request, HttpServletResponse response)
       throws ServletException, IOException {
      doGet(request, response);
     }
    }
     
    2、前台页面调用;举例:
    <img src="http://127.0.0.1:8080/RandomCodeCtrl" />
     
    3、验证输入信息和随机生成的图片显示的内容相同:
      在RandomCode.java代码中随机生成的图片之前,就把随机生成的图片显示的内容放在session中;所以只需判断session中getAttribute("Rand")的值和用户页面输入的验证码值相等即可。
    如果一件事情你觉得难的完不成,你可以把它分为若干步,并不断寻找合适的方法。最后你发现你会是个超人。不要给自己找麻烦,但遇到麻烦绝不怕,更不要退缩。 电工查找电路不通点的最快方法是:分段诊断排除,快速定位。你有什么启示吗? 求知若饥,虚心若愚。 当你对一个事情掌控不足的时候,你需要做的就是“梳理”,并制定相应的规章制度,并使资源各司其职。
  • 相关阅读:
    Java之Jacob调用COM接口DLL-----------------------------------dm。dll
    mac版idea报错:Information:java: javacTask: 源发行版 1.8 需要目标发行版 1.8
    Spring-data-jpa 学习笔记
    Mac下IntelliJ IDEA快捷键大全
    mac怎么快速回到桌面 隐藏所有窗口
    Idea下安装Lombok插件
    spring注解第05课 FactoryBean
    apt 软件安装问题
    常用软件和库安装
    openMP---第一篇
  • 原文地址:https://www.cnblogs.com/wvqusrtg/p/5105362.html
Copyright © 2011-2022 走看看