预览加载中,请您耐心等待几秒...
1/9
2/9
3/9
4/9
5/9
6/9
7/9
8/9
9/9

在线预览结束,喜欢就下载吧,查找使用更方便

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

Java验证码生成图片 PublicclassRandomValidateCode{publicstaticfinalStringRANDOMCODEKEY="RANDOMVALIDATECODEKEY";//放到session中的keyprivateRandomrandom=newRandom();privateStringrandString="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";//随机产生的字符串privateintwidth=80;//图片宽privateintheight=26;//图片高privateintlineSize=40;//干扰线数量privateintstringNum=4;//随机产生字符数量/**获得字体*/privateFontgetFont(){returnnewFont("Fixedsys",Font.CENTER_BASELINE,18);}/**获得颜色*/privateColorgetRandColor(intfc,intbc){if(fc>255)fc=255;if(bc>255)bc=255;intr=fc+random.nextInt(bc-fc-16);intg=fc+random.nextInt(bc-fc-14);intb=fc+random.nextInt(bc-fc-18);returnnewColor(r,g,b);}/***生成随机图片*/publicvoidgetRandcode(HttpServletRequestrequest,HttpServletResponseresponse){HttpSessionsession=request.getSession();//BufferedImage类是具有缓冲区的Image类,Image类是用于描述图像信息的类BufferedImageimage=newBufferedImage(width,height,BufferedImage.TYPE_INT_BGR);Graphicsg=image.getGraphics();//产生Image对象的Graphics对象,改对象可以在图像上进行各种绘制操作g.fillRect(0,0,width,height);g.setFont(newFont("TimesNewRoman",Font.ROMAN_BASELINE,18));g.setColor(getRandColor(110,133));//绘制干扰线for(inti=0;i<=lineSize;i++){drowLine(g);}//绘制随机字符StringrandomString="";for(inti=1;i<=stringNum;i++){randomString=drowString(g,randomString,i);}session.removeAttribute(RANDOMCODEKEY);session.setAttribute(RANDOMCODEKEY,randomString);System.out.println(randomString);g.dispose();try{ImageIO.write(image,"JPEG",response.getOutputStream());//将内存中的图片通过流动形式输出到客户端}catch(Exceptione){e.printStackTrace();}}/**绘制字符串*/privateStringdrowString(Graphicsg,StringrandomString,inti){g.setFont(getFont());g.setColor(newColor(random.nextInt(101),random.nextInt(111),random.nextInt(121)));Stringrand=String.valueOf(getRandomString(random.nextInt(randString.length())));randomString+=rand;g.translate(random.nextInt(3),random.nextInt(3));g.drawString(rand,13*i,16);returnrandomString;}/**绘制干扰线*/privatevoiddrowLine(Graphicsg){intx=random.nextInt(width);inty=random.nextInt(height);intxl=random.nextInt(13);intyl=random.nextInt(15);g.drawLine(x,y,x+xl