action中的代码:
@Controller(value = "randomPictureAction")
public class RandomPictureAction extends BaseAction {
private static final long serialVersionUID = -6950908478971552308L;
private ByteArrayInputStream inputStream;
public String execute() throws Exception {
// 在内存中创建图象
int width = 85, height = 20;
BufferedImage image = new BufferedImage(width, height,BufferedImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
// 生成随机类
Random random = new Random();
// 设定背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
// 设定字体
g.setFont(new Font("Times New Roman", Font.PLAIN, 18));
// 随机产生155条干扰线,使图象中的认证码不易被其它程序探测到
g.setColor(getRandColor(160, 200));
for (int i = 0; i < 155; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x, y, x + xl, y + yl);
}
// 取随机产生的认证码(6位数字)
String sRand = "";
for (int i = 0; i < 6; i++) {
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 将认证码显示到图象中
g.setColor(new Color(20 + random.nextInt(110), 20 + random
.nextInt(110), 20 + random.nextInt(110)));
// 调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成
g.drawString(rand, 13 * i + 6, 16);
}
// 将认证码存入SESSION
ActionContext.getContext().getSession().put("rand", sRand);
//this.getRequest().setAttribute("rand", sRand);
this.str = sRand;
System.out.println("rand="+sRand);
// 图象生效
g.dispose();
ByteArrayOutputStream output = new ByteArrayOutputStream();
ImageOutputStream imageOut = ImageIO.createImageOutputStream(output);
ImageIO.write(image, "JPEG", imageOut);
imageOut.close();
ByteArrayInputStream input = new ByteArrayInputStream(output .toByteArray());
this.setInputStream(input);
return SUCCESS;
}
/*
* 给定范围获得随机颜色
*/
private Color getRandColor(int fc, int bc) {
Random random = new Random();
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public void setInputStream(ByteArrayInputStream inputStream) {
this.inputStream = inputStream;
}
public ByteArrayInputStream getInputStream() {
return inputStream;
}
}
xml中的代码实现:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- Struts2配置文件 -->
<struts>
<!-- 景阳岗酒业客户留言管理 -->
<package name="randpicture" namespace="/" extends="struts-default,json-default">
<!-- 工具action -->
<action name="rand" class="randomPictureAction">
<result name="success" type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package>
</struts>
jsp页面中的代码实现:
<script type="text/javascript">
function changeValidateCode(obj) {
var currentTime= new Date().getTime();
obj.src = "rand?d=" + currentTime;
}
</script>
<img src="rand" onclick="changeValidateCode(this)" />
分享到:
相关推荐
Struts+jsp版的随机验证和servlet+jsp版的随机验证码
生成随机验证码 可在struts中使用
众所周知,验证码在我们的生活中都是非常常见的,很多公司都在各种折腾各种各样的验证码,这里简要的用一个小案例来实现验证码的功能(ps:其实我挺讨厌验证码这个东西的)。 今天分享的是通过ajax来动态的验证验证码...
Struts 2.0技术综合应用,包括上传图片功能,明文加密算法SHA-1和MD5,上传用户头像,根据action随机生成验证码,用链接实现数据的分页处理,以及拦截器和Logger日志框架的引入,总之相当强大,学习Struts 2.0,把这...
随机彩色防识别验证码 内置管理员账户,可以创建、删除、修改用户,并查看用户列表和单个用户详情 普通用户账户,记录用户名和密码在SQL数据库中,支持用户头像上传 普通用户能实现发新帖、回帖、浏览帖子,但不...
20.5.4 生成随机验证码的imgNum类 358 20.5.5 用户登录页面index.jsp 359 20.5.6 验证用户登录信息的Servlet类login 360 20.6 显示宠物信息模块的实现 363 20.6.1 对应宠物的实体类User 363 20.6.2 定义对宠物信息...
提供用户登录功能,随机生成验证码验证 4 后台管理模块 提供日志的查询、删除、修改、新增功能 提供分页功能 1.3. 运行环境 1. 软件环境 分类 名称 版本 语种 操作系统 Windows XP 简体中文 数据库平台 Oracle10g ...
SSH 小例子,一个论坛用户登录的例子,可以生成随机验证码,附带PDF教程,需要根据教程创建Mysql表,然后发布到Tomcat,就可以运行了
这是使用spring、struts2、hibernate整合后的网上考试系统的全部代码,包括管理员登录、学生登录以及登录验证与验证码验证,试卷随机生成,试卷保存与查看等内容。想要了解更多的内容,请赶紧下载!
利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实世界模型 基于EJB的真实世界模型,附源代码,部分功能需JSP配合完成。 J2ME优化压缩PNG文件 4个...
利用随机函数抽取幸运数字 简单 EJB的真实世界模型(源代码) 15个目标文件 摘要:Java源码,初学实例,基于EJB的真实世界模型 基于EJB的真实世界模型,附源代码,部分功能需JSP配合完成。 J2ME优化压缩PNG文件 4个...