第一个:汉子型的验证码 从下面开始考呗 ---------------------------------------------》》》》
<%@ page lang ge="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%
String path = reqst.getContextPath();
String basePath = reqst.getScheme()+"://"+reqst.getServerName()+":"+reqst.getServerPort()+path+"/";
%>
<!DOCTYPE HTML P LIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'MyJsp.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!
//create by smallnest
//email: [email protected]
//website:www.k ff.com
//生成随机颜色
Color getRandColor(Random random,int fc,int bc)
{
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);
}
%>
<%
// 设置图片的长宽
int width=176, height=30;
//设置备选汉字,剔除一些不雅的汉字
String base = "/的/一/了/是/我/不/在/人/们/有/来/他/这/上/着/个/地/到/大/里/说/就/去/子/得/也/和/那/要/下/看/天/时/过/出/小/么/起/你/都/把/好/还/多/没/为/又/可/家/学/只/以/主/会/样/年/想/生/同/老/中/十/从/自/面/前/头/道/它/后/然/走/很/像/见/两/用/她/国/动/进/成/回/什/边/作/对/开/而/己/些/现/山/民/候/经/发/工/向/事/命/给/长/水/几/义/三/声/于/高/手/知/理/眼/志/点/心/战/二/问/但/身/方/实/吃/做/叫/当/住/听/革/打/呢/真/全/才/四/已/所/敌/之/最/光/产/情/路/分/总/条/白/话/东/席/次/亲/如/被/花/口/放/儿/常/气/五/第/使/写/军/吧/文/运/再/果/怎/定/许/快/明/行/因/别/飞/外/树/物/活/部/门/无/往/船/望/新/带/队/先/力/完/却/站/代/员/机/更/九/您/每/风/级/跟/笑/啊/孩/万/少/直/意/夜/比/阶/连/车/重/便/斗/马/哪/化/太/指/变/社/似/士/者/干/石/满/日/决/百/原/拿/群/究/各/六/本/思/解/立/河/村/八/难/早/论/吗/根/共/让/相/研/今/其/书/坐/接/应/关/信/觉/步/反/处/记/将/千/找/争/领/或/师/结/块/跑/谁/草/越/字/加/脚/紧/爱/等/习/阵/怕/月/青/半/火/法/题/建/赶/位/唱/海/七/女/任/件/感/准/张/团/屋/离/色/脸/片/科/倒/睛/利/世/刚/且/由/送/切/星/导/晚/表/够/整/认/响/雪/流/未/场/该/并/底/深/刻/平/伟/忙/提/确/近/亮/轻/讲/农/古/黑/告/界/拉/名/呀/土/清/阳/照/办/史/改/历/转/画/造/嘴/此/治/北/必/服/雨/穿/内/识/验/传/业/菜/爬/睡/兴/形/量/咱/观/苦/体/众/通/冲/合/破/友/度/术/饭/公/旁/房/极/南/枪/读/沙/岁/线/野/坚/空/收/算/至/政/城/劳/落/钱/特/围/弟/胜/教/热/展/包/歌/类/渐/强/数/乡/呼/性/音/答/哥/际/旧/神/座/章/帮/啦/受/系/令/跳/非/何/牛/取/入/岸/敢/掉/忽/种/装/顶/急/林/停/息/句/区/衣/般/报/叶/压/慢/叔/背/细";
//备选汉字的长度
int length = base.length();
//创建内存图像
B?redImage image = new B?redImage(width, height, B?redImage.TYPE_INT_RGB);
// 获取图形上下文
Graphics g = image.getGraphics();
//创建随机类的实例
Random random = new Random();
// 设定图像背景色(因为是做背景,所以偏淡)
g.setColor(getRandColor(random,200,250));
g.fillRect(0, 0, width, height);
//备选字体
String[] fontTypes = {"/宋/体","/新/宋/体","/黑/体","/楷/体","/隶/书"};
int fontTypesLength = fontTypes.length;
//在图片背景上增加噪点
g.setColor(getRandColor(random,160,200));
g.setFont(new Font("Times New Roman",Font.PLAIN,14));
for (int i=0;i<6;i++)
{
g.drawString("*********************************************",0,5*(i+2));
}
//取随机产生的认证码(6个汉字)
//保存生成的汉字字符串
String sRand="";
for (int i=0;i<6;i++)
{
int start = random.nextInt(length);
String rand=base.s string(start,start+1);
sRand+=rand;
//设置字体的颜色
g.setColor(getRandColor(random,10,150));
//设置字体
g.setFont(new Font(fontTypes[random.nextInt(fontTypesLength)],Font.BOLD,18 + random.nextInt(6)));
//将此汉字画到图片上
g.drawString(rand,24*i+ 10 + random.nextInt(8),24);
}
//将认证码存入session
session.setAttribute("rand",sRand);
g.dispose();
//输出图象到页面
ImageIO.write(image, "JPEG", response.getOutputStream());
out.clear();
out = pageContext.pushBody();
%>
</body>
</html>
--------------------结束
第二个 从下面开始拷贝:数字型的验证码
<%@ page import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>
<%@ page import="java.io.OutputStream" %>
<%!
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);
}
%>
<%
try{
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
OutputStream os=response.getOutputStream();
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));
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);
}
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valOf(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.setAttribute("rand",sRand);
g.dispose();
ImageIO.write(image, "JPEG",os);
os.flush();
os.close();
os=null;
response.flushBuffer();
out.clear();
out = pageContext.pushBody();
}
catch(IllegalStateException e)
{
System.out.println(e.getMessage());
e.printStackTrace();
}%>