-- 用于生成验证码

publicclassRandomNumUtil{privateByteArrayInputStreamp_w_picpath;//图像privateStringstr;//验证码publicRandomNumUtil(){init();}privatevoidinit(){//在内存中创建图象intwidth=85,height=20;BufferedImagep_w_picpath=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);//获取图形上下文Graphicsg=p_w_picpath.getGraphics();//生成随机类Randomrandom=newRandom();//设定背景色g.setColor(getRandColor(200,250));g.fillRect(0,0,width,height);//设定字体g.setFont(newFont("TimesNewRoman",Font.PLAIN,12));//随机产生155条干扰线,使图象中的认证码不易被其它程序探测到g.setColor(getRandColor(160,200));for(inti=0;i<155;i++){intx=random.nextInt(width);inty=random.nextInt(height);intxl=random.nextInt(12);intyl=random.nextInt(12);g.drawLine(x,y,x+xl,y+yl);}//取随机产生的认证码(6位数字)StringsRand="";for(inti=0;i<6;i++){Stringrand=getVerify(random.nextInt(3));sRand+=rand;//将认证码显示到图象中g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));//调用函数出来的颜色相同,可能是因为种子太接近,所以只能直接生成g.drawString(rand,13*i+6,random.nextInt(10)+10);}//赋值验证码this.str=sRand;//图象生效g.dispose();ByteArrayInputStreaminput=null;ByteArrayOutputStreamoutput=newByteArrayOutputStream();try{ImageOutputStreamp_w_picpathOut=ImageIO.createImageOutputStream(output);ImageIO.write(p_w_picpath,"JPEG",p_w_picpathOut);p_w_picpathOut.close();input=newByteArrayInputStream(output.toByteArray());}catch(Exceptione){System.out.println("验证码图片产生出现错误:"+e.toString());}this.p_w_picpath=input;/*赋值图像*/}/*随机产生颜色*/privateColorgetRandColor(intfc,intbc){Randomrandom=newRandom();if(fc>255)fc=255;if(bc>255)bc=255;intr=fc+random.nextInt(bc-fc);intg=fc+random.nextInt(bc-fc);intb=fc+random.nextInt(bc-fc);returnnewColor(r,g,b);}/***生成验证码*@parami0为小写字母*1为大写字母*2为普通数字*@return*/privateStringgetVerify(inti){Stringstr="";if(i==0){charc='a';c=(char)(c+(int)(Math.random()*26));str=c+"";}elseif(i==1){charc='A';c=(char)(c+(int)(Math.random()*26));str=c+"";}elseif(i==2){str=newRandom().nextInt(10)+"";}returnstr;}publicByteArrayInputStreamgetImage(){returnp_w_picpath;}publicvoidsetImage(ByteArrayInputStreamp_w_picpath){this.p_w_picpath=p_w_picpath;}publicStringgetStr(){returnstr;}publicvoidsetStr(Stringstr){this.str=str;}

-- Struts2

publicclassUtilActionextendsActionSupport{privateByteArrayInputStreaminputStream;@OverridepublicStringexecute()throwsException{//TODOAuto-generatedmethodstubRandomNumUtilrdnu=newRandomNumUtil();this.setInputStream(rdnu.getImage());//取得带有随机字符串的图片ActionContext.getContext().getSession().put("validateCode",rdnu.getStr());//取得随机字符串放入HttpSessionreturnSUCCESS;}publicByteArrayInputStreamgetInputStream(){returninputStream;}publicvoidsetInputStream(ByteArrayInputStreaminputStream){this.inputStream=inputStream;}}

-- JQuery

$(function(){$("#randomCode").attr("src","verify");$("#refresh").click(function(){$("#randomCode").attr("src","verify");});})

-- Struts2 xml配置

<actionname="verify"class="verify.UtilAction"><resulttype="stream"><paramname="contentType">p_w_picpath/jpeg</param><paramname="inputName">inputStream</param></result></action>

-- jsp页面

<body><imgsrc=""width=150height="50"alt="验证码图片"id="randomCode"/><spanid="refresh"style="cursor:pointer;">换张图片</span></body>

--效果如图