//验证码
    public String LoginCode(){
        HttpServletResponse response = ServletActionContext.getResponse();
        HttpServletRequest request = ServletActionContext.getRequest();
        //设置返回头
        response.setHeader("Cache-Control", "no-cache");  
        response.setHeader("Cache-Control", "no-store");  
        response.setHeader("Pragma", "no-cache");  
        response.setDateHeader("Expires", 0);
        //读取图片获得图片对象
        ImageIcon lo_img = new ImageIcon(ServletActionContext.getServletContext().getRealPath("/img/LoginCode.jpg"));
        //ͨ获得图片蒙版对象?
        BufferedImage bfimg = new BufferedImage(lo_img.getIconWidth(), lo_img.getIconHeight(), BufferedImage.TYPE_3BYTE_BGR);
        //ͨ获得画笔对象
        Graphics gh = bfimg.getGraphics();
        //将图片和画笔连接
        gh.drawImage(lo_img.getImage(), 0, 0, null);
        
        //生成验证码
        String str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char[] num = {\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\',\'0\'};
        char[] Capital = str.toCharArray();
        //获得随机数对象
        Random rd = new Random();
        //获得Stringbuf对象
        StringBuffer lo_str = new StringBuffer();
        //生成验证码
        for(int j = 0 ; j < 1 ;j++){
            lo_str.append(Capital[rd.nextInt(Capital.length)]);
        }
        for(int i = 0 ; i < 1 ;i++){
                lo_str.append(num[rd.nextInt(num.length)]);
        }
        for(int q = 0 ; q < 1 ;q++){
            lo_str.append(Capital[rd.nextInt(Capital.length)]);
        }
        for(int w = 0 ; w < 1 ;w++){
                lo_str.append(num[rd.nextInt(num.length)]);
        }
    
        //
        gh.setFont(new Font("����",Font.BOLD, 30));
        //生成干扰吗
        gh.drawString(lo_str.toString(), lo_img.getIconWidth()/4, lo_img.getIconHeight()/2+10);
        //���ͼƬ
        try {
            ImageIO.write(bfimg, "jpg", response.getOutputStream());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
        
    }

 

版权声明:本文为softbreak原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/softbreak/articles/10918111.html