java登录验证码

java验证码

直接上代码啦,其实验证码在网上一抓一大把,之所以记录一下是标记几个自己花了点心思去修改的地方:

一个是修改验证码的字体大小,一个是在ie兼容问题,本人只在ie11试验过

package com.greatway.controller;

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

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value="/logincheck",produces="text/json;charset=UTF-8")
public class LoginCheckController {
    //图片高度
    private static final int IMG_HEIGHT = 100;
    //图片宽度
    private static final int IMG_WIDTH = 30;
    //验证码长度
    private static final int CODE_LEN = 4;

    @RequestMapping("/checkImg")
    public void checkImg(HttpServletRequest request,HttpServletResponse response){
        try {
            //用于绘制图片,设置图片长宽及类型(RGBBufferedImage bi = new BufferedImage(IMG_HEIGHT, IMG_WIDTH, CODE_LEN);
            //获取绘图工具
            Graphics graphics = bi.getGraphics();
            graphics.setColor(new Color(100, 230, 200));
            graphics.fillRect(0, 0, 100, 30);//填充矩形区域

            //验证码所用到的字符
            char[] codeChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
            String captcha = "";//存放生成的验证码
            Random random = new Random();
            for(int i=0; i<CODE_LEN; i++){//将验证码绘制到图片上
                int index = random.nextInt(codeChar.length);
                //随机生成验证码的颜色
                graphics.setColor(new Color(random.nextInt(150), random.nextInt(200), random.nextInt(255)));
                //设置字体,样式,大小
                graphics.setFont(new Font("楷体", Font.BOLD, 20));
                //将一个字符绘制到图片上,并制定位置(xy坐标)
                graphics.drawString(codeChar[index] + "", (i*20)+15, 20);
                captcha += codeChar[index];
            }
            //将生成的验证码code放入sessionrequest.getSession().setAttribute("code", captcha);
            //通过ImageIO将图片输出
            ImageIO.write(bi, "JPG", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e){
            e.printStackTrace();
        }
    }
}

... prompt'''

html code

<table>
 <td style="width:80px;">&nbsp;&nbsp;验证码:</td>
 <td>
    <div style="float:left"><input type="text" name="code" id="code" style="width:190px;"></div>
    <div style="float:left;margin-left:5px;margin-top:3px;"><img alt="验证码" id="scode" src="<%=request.getContextPath() %>/logincheck/checkImg" style="width:60px;height:25px;">
    <span onclick="flushCode()">
        <img alt="" src="resources/images/flush.png" style="width:20px;height:20px;">
    </span>
    </div>
</td>
<table>
<script type="text/javascript">
    function flushCode() {
    // 每次刷新的时候生成随机数,防止浏览器缓存刷新失败,当然这个地方需要重点标明一下,
    //之前我用的new Date(),谷歌跟搜狐都是ok的可以刷新,but ie11不行啊!!!用随机数就ok了
    //为啥???做了个实验,如果获取到秒的话ie也是ok的,估计new Date太粗糙了,为了方便还是跟我一起用随机数吧咩
/**var now = new Date();  
   now.setDate(now.getDate());
   var year = now.getFullYear();//年  
   var month = now.getMonth() + 1;//月  
   var day = now.getDate();//日
   var hh = now.getHours();//时
   var mm = now.getMinutes();
   var ss = now.getSeconds();
   var time = year+"-"+month+"-"+day+" "+hh+":"+mm+":"+ss;*/
    document.getElementById("scode").src = "<%=request.getContextPath()%>/logincheck/checkImg?flag=" + Math.random() ;

}
        </script>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值