六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

使用Google工具生成随机验证码

[复制链接]

升级  12.33%

67

主题

67

主题

67

主题

举人

Rank: 3Rank: 3

积分
237
 楼主| 发表于 2013-2-1 09:44:56 | 显示全部楼层 |阅读模式
http://www.open-china.net/blog/46973.html
 
很多网站都有验证码认证的功能。生成随机数,需要用户自己填写,之后进行认证。这是为了防止非法使用者利用工具工具网站。
以前有人自己写个工具类将生成的数字输出成byte[]类型,之后再输出到前台页面。
这几天看到了Google Code有个开源项目kaptcha,用来生成随机验证码。十分好用。分享给大家
首先从google下载组件()
解压缩后将kaptcha-X.jar包加入到自己项目的classpath中。
 
初始化该类代码如下
static DefaultKaptcha defaultKaptcha = new DefaultKaptcha();static {    defaultKaptcha.setConfig(new Config(new Properties()));} 加载默认配置
使用如下
BufferedImage bufferedImage = defaultKaptcha.createImage(随机数);ByteArrayOutputStream out = new ByteArrayOutputStream();try {    ImageIO.write(bufferedImage, "jpg", out);} catch (IOException e) {    e.printStackTrace();}File file = new File("c:/1.jpg");try {    FileOutputStream fileOut = new FileOutputStream(file);    fileOut.write(out.toByteArray());} catch (FileNotFoundException e) {} catch (IOException e) {}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表