package org.crazyit.user.web;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.util.*;
import javax.imageio.*;
/**
* Description:
* <br/>Copyright (C), 2001-2010, Yeeku.H.Lee
* <br/>This program is protected by copyright laws.
* <br/>Program Name:
* <br/>Date:
* @author Yeeku.H.Lee kongyeeku@163.com
* @version 1.0
*/
public class AuthImg extends HttpServlet
{
//定义图形验证码中绘制字符的字体
private final Font mFont =
new Font("Arial Black", Font.PLAIN, 16);
//定义图形验证码的大小
private final int IMG_WIDTH = 100;
private final int IMG_HEIGTH = 18;
//定义一个获取随机颜色的方法
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);
}
//重写service方法,生成对客户端的响应
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
//设置禁止缓存
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
BufferedImage image = new BufferedImage
(IMG_WIDTH , IMG_HEIGTH , BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200 , 250));
//填充背景色
g.fillRect(1, 1, IMG_WIDTH - 1, IMG_HEIGTH - 1);
//为图形验证码绘制边框
g.setColor(new Color(102 , 102 , 102));
g.drawRect(0, 0, IMG_WIDTH - 1, IMG_HEIGTH - 1);
g.setColor(getRandColor(160,200));
//生成随机干扰线
for (int i = 0 ; i < 80 ; i++)
{
int x = random.nextInt(IMG_WIDTH - 1);
int y = random.nextInt(IMG_HEIGTH - 1);
int xl = random.nextInt(6) + 1;
int yl = random.nextInt(12) + 1;
g.drawLine(x , y , x + xl , y + yl);
}
g.setColor(getRandColor(160,200));
//生成随机干扰线
for (int i = 0 ; i < 80 ; i++)
{
int x = random.nextInt(IMG_WIDTH - 1);
int y = random.nextInt(IMG_HEIGTH - 1);
int xl = random.nextInt(12) + 1;
int yl = random.nextInt(6) + 1;
g.drawLine(x , y , x - xl , y - yl);
}
//设置绘制字符的字体
g.setFont(mFont);
//用于保存系统生成的随机字符串
String sRand = "";
for (int i = 0 ; i < 6 ; i++)
{
String tmp = getRandomChar();
sRand += tmp;
//获取随机颜色
g.setColor(new Color(20 + random.nextInt(110)
,20 + random.nextInt(110)
,20 + random.nextInt(110)));
//在图片上绘制系统生成的随机字符
g.drawString(tmp , 15 * i + 10,15);
}
//获取HttpSesssion对象
HttpSession session = request.getSession(true);
//将随机字符串放入HttpSesssion对象中
session.setAttribute("rand" , sRand);
g.dispose();
//向输出流中输出图片
ImageIO.write(image, "JPEG", response.getOutputStream());
}
//定义获取随机字符串方法
private String getRandomChar()
{
//生成一个0、1、2的随机数字
int rand = (int)Math.round(Math.random() * 2);
long itmp = 0;
char ctmp = '\u0000';
switch (rand)
{
//生成大写字母
case 1:
itmp = Math.round(Math.random() * 25 + 65);
ctmp = (char)itmp;
return String.valueOf(ctmp);
//生成小写字母
case 2:
itmp = Math.round(Math.random() * 25 + 97);
ctmp = (char)itmp;
return String.valueOf(ctmp);
//生成数字
default :
itmp = Math.round(Math.random() * 9);
return itmp + "";
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
实现0配置的SSH框架
共81个文件
jar:31个
xml:13个
class:8个
5星 · 超过95%的资源 需积分: 3 29 下载量 44 浏览量
2010-03-26
14:00:44
上传
评论
收藏 12.35MB RAR 举报
温馨提示
实现0配置的SSH框架。采用最新的STRUTS2+SPRING+HIBERNATE开发
资源推荐
资源详情
资源评论
收起资源包目录
reg_login_Eclipse.rar (81个子文件)
reg_login_Eclipse
.project 1KB
.mymetadata 324B
WebRoot
login.jsp 1007B
json2.js 17KB
success.jsp 915B
WEB-INF
decorators.xml 454B
web.xml 2KB
decorators
link.html 600B
panel.jsp 364B
main.jsp 1KB
main.css 818B
book.html 507B
applicationContext.xml 4KB
lib
freemarker-2.3.13.jar 850KB
c3p0-0.9.1.jar 594KB
javassist-3.4.GA.jar 460KB
slf4j-api-1.5.6.jar 22KB
mysql-connector-java-3.1.10-bin.jar 409KB
commons-collections-3.2.jar 558KB
jta-1.1.jar 13KB
sitemesh-2.4.jar 177KB
hibernate3.jar 2.64MB
jstl.jar 20KB
ognl-2.6.11.jar 164KB
xwork-2.1.2.jar 2.18MB
jsonplugin-0.33.jar 45KB
commons-digester-1.8.jar 140KB
commons-logging-api-1.1.jar 44KB
struts2-spring-plugin-2.1.6.jar 9KB
commons-discovery-0.4.jar 75KB
commons-beanutils-1.7.0.jar 184KB
commons-collections-3.1.jar 546KB
struts2-core-2.1.6.jar 719KB
commons-fileupload-1.2.1.jar 56KB
commons-lang-2.3.jar 240KB
commons-codec-1.3.jar 46KB
slf4j-nop-1.5.6.jar 5KB
dom4j-1.6.1.jar 307KB
struts2-sitemesh-plugin-2.1.6.jar 16KB
asm.jar 26KB
struts2-convention-plugin-2.1.6.jar 61KB
struts2-config-browser-plugin-2.1.6.jar 38KB
antlr-2.7.6.jar 433KB
spring.jar 2.81MB
classes
globalMessages.properties 316B
org
crazyit
user
dao
impl
UserDaoHibernate.class 3KB
UserDao.class 639B
action
UserAction-regist-validation.xml 810B
UserAction-validation.xml 1KB
UserAction.class 3KB
ValidateAction.class 2KB
model
User.class 1KB
User.hbm.xml 751B
web
AuthImg.class 4KB
service
impl
UserManagerImpl.class 2KB
UserManager.class 321B
struts.xml 513B
ehcache.xml 4KB
globalMessages_zh_CN.properties 674B
index.jsp 834B
regist.jsp 2KB
META-INF
MANIFEST.MF 39B
error.jsp 1KB
images
main.css 818B
prototype-1.6.0.3.js 127KB
.myeclipse
src
globalMessages.properties 316B
org
crazyit
user
dao
UserDao.java 1KB
impl
UserDaoHibernate.java 2KB
action
UserAction.java 3KB
UserAction-regist-validation.xml 810B
UserAction-validation.xml 1KB
ValidateAction.java 2KB
model
User.java 1KB
User.hbm.xml 751B
web
AuthImg.java 4KB
service
UserManager.java 899B
impl
UserManagerImpl.java 2KB
struts.xml 513B
ehcache.xml 4KB
globalMessages_zh_CN.properties 674B
.classpath 3KB
共 81 条
- 1
资源评论
- xxxmoumou2013-06-14不错好东西 可以用
yy0181130
- 粉丝: 16
- 资源: 22
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功