spirng mvc 中使用验证码

生成验证码方法:

@RequestMapping("/vCode")
    @ResponseBody
    public Map<String, String> vCode(HttpServletRequest request,HttpServletResponse response){
        //设置不缓存图片
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "No-cache");
        response.setDateHeader("Expires", 0);
        //设置生成类型
        response.setContentType("image/jpeg");
        int width = 85;
        int height = 25;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        Graphics2D g2d = (Graphics2D)g;
        Font font = new Font("正楷", Font.BOLD, 16);
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        g.setFont(font);
        g.setColor(getRandColor(180, 200));
        String vCode = "";
        String msg = "";
        Random random= new Random ();
        int length = Math.abs(random.nextInt(2));
        for (int i = 0; i < 4 +length; i++) {
            random = new Random ();
            int check = Math.abs(random.nextInt(3));
            if (check == 0) {
                msg = StringUtil.getNum()+"";
            }
            if (check == 1) {
                msg = StringUtil.getChar()+"";
            }
            if (check == 2) {
                msg = StringUtil.getChinese();
            }
            Color color = new Color(20+random.nextInt(110),20+random.nextInt(110),random.nextInt(110));
            g.setColor(color);
            g.drawString(msg, 15*i+5, 14);
            vCode += msg; 
        }
        HttpSession session = request.getSession(true);
        session.setAttribute("vCode", vCode);
        g.dispose();
        try {
            ImageIO.write(image, "JPEG", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 public Color getRandColor(int s,int e){  
            Random random=new Random ();  
            if(s>255) s=255;  
            if(e>255) e=255;  
            int r,g,b;  
            r=s+random.nextInt(e-s);    //随机生成RGB颜色中的r值  
            g=s+random.nextInt(e-s);    //随机生成RGB颜色中的g值  
            b=s+random.nextInt(e-s);    //随机生成RGB颜色中的b值  
            return new Color(r,g,b);  
     }  
public class StringUtil {

    public static void main(String[] args) {
        System.out.println(getValidCode());
    }

    public static String getValidCode() {
        String ret = "";
        Random random = new Random();
        for (int i = 0; i < 4 + Math.abs(random.nextInt(3)); i++) {
            int check = Math.abs(random.nextInt(3));
            if (check == 0) {
                ret += getNum();
            }
            if (check == 1) {
                ret += getChar();
            }
            if (check == 2) {
                ret += getChinese();
            }
            
        }
        return ret;
    }

    public static String getChinese() {
        Random random = new Random();
        byte[] b = new byte[2];
        b[0] = (new Integer(176 + Math.abs(random.nextInt(39))).byteValue());// 获取高位值
        b[1] = (new Integer(161 + Math.abs(random.nextInt(93))).byteValue());// 获取低位值
        try {
            return new String(b, "GBk"); // 转成中文
        } catch (UnsupportedEncodingException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    public static char getChar() {
        Random random = new Random();
        int index = Math.abs(random.nextInt(26));
        return (char) (index + 'A');
    }

    public static int getNum() {
        Random random = new Random();
        int index = Math.abs(random.nextInt(10));
        return index;
    }

}

 

posted @ 2016-04-23 11:45  phyxis_xu  阅读(217)  评论(0编辑  收藏  举报