Jsp图片验证码及刷新.docx
- 文档编号:12880778
- 上传时间:2023-04-22
- 格式:DOCX
- 页数:19
- 大小:17.03KB
Jsp图片验证码及刷新.docx
《Jsp图片验证码及刷新.docx》由会员分享,可在线阅读,更多相关《Jsp图片验证码及刷新.docx(19页珍藏版)》请在冰豆网上搜索。
Jsp图片验证码及刷新
1.图片验证码的实现主要的技术点是如何生成一个图片。
生成图片可以使用java.awt包下的类来实现。
我们先写一个简单的生成图片的程序HelloImage.java。
以下是代码部分。
(是我在做留言板项目时的部分代码)
"HelloImage.java"
viewplaincopytoclipboardprint?
packageliuyanban;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
/**
*生成图片
*/
publicclassHelloImage{
publicstaticvoidmain(String[]args){
BufferedImageimage=newBufferedImage(80,50,
BufferedImage.TYPE_INT_RGB);//建立BufferedImage对象。
指定图片的长度宽度和色彩。
Graphicsg=image.getGraphics();//取得Graphics对象,用来绘制图片。
g.setColor(newColor(255,255,255));//绘制图片背景和文字。
g.fillRect(0,0,80,25);
g.setColor(newColor(0,0,0));
g.drawString("HelloImage",6,16);
g.dispose();//释放Graphics对象所占用的资源。
try{
ImageIO.write(image,"jpeg",newFile("C:
\\helloImage.jpeg"));//通过ImageIO对象的write静态方法将图片输出。
}catch(IOExceptione){
e.printStackTrace();
}
}
}
packageliuyanban;
importjava.awt.Color;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.File;
importjava.io.IOException;
importjavax.imageio.ImageIO;
/**
*生成图片
*/
publicclassHelloImage{
publicstaticvoidmain(String[]args){
BufferedImageimage=newBufferedImage(80,50,
BufferedImage.TYPE_INT_RGB);//建立BufferedImage对象。
指定图片的长度宽度和色彩。
Graphicsg=image.getGraphics();//取得Graphics对象,用来绘制图片。
g.setColor(newColor(255,255,255));//绘制图片背景和文字。
g.fillRect(0,0,80,25);
g.setColor(newColor(0,0,0));
g.drawString("HelloImage",6,16);
g.dispose();//释放Graphics对象所占用的资源。
try{
ImageIO.write(image,"jpeg",newFile("C:
\\helloImage.jpeg"));//通过ImageIO对象的write静态方法将图片输出。
}catch(IOExceptione){
e.printStackTrace();
}
}
}
viewplaincopytoclipboardprint?
********************************************************
********************************************************viewplaincopytoclipboardprint?
2.生成验证码图片的类
2.生成验证码图片的类viewplaincopytoclipboardprint?
packageliuyanban;
importjava.awt.Color;
importjava.awt.Font;
importjava.awt.Graphics;
importjava.awt.image.BufferedImage;
importjava.io.IOException;
importjava.util.Random;
importjavax.imageio.ImageIO;
importjavax.servlet.http.HttpServletResponse;
/**
*生成验证码图片的类
*/
publicclassRandImgCreater{
privatestaticfinalStringCODE_LIST="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
privateHttpServletResponseresponse=null;
privatestaticfinalintHEIGHT=20;
privatestaticfinalintFONT_NUM=4;
privateintwidth=0;
privateintiNum=0;
privateStringcodeList="";
privatebooleandrawBgFlag=false;
privateintrBg=0;
privateintgBg=0;
privateintbBg=0;
publicRandImgCreater(HttpServletResponseresponse){
this.response=response;
this.width=13*FONT_NUM+12;
this.iNum=FONT_NUM;
this.codeList=CODE_LIST;
}
publicRandImgCreater(HttpServletResponseresponse,intiNum,StringcodeList){
this.response=response;
this.width=13*iNum+12;
this.iNum=iNum;
this.codeList=codeList;
}
publicStringcreateRandImage(){
BufferedImageimage=newBufferedImage(width,HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphicsg=image.getGraphics();
Randomrandom=newRandom();
if(drawBgFlag){
g.setColor(newColor(rBg,gBg,bBg));
g.fillRect(0,0,width,HEIGHT);
}else{
g.setColor(getRandColor(200,250));
g.fillRect(0,0,width,HEIGHT);
for(inti=0;i<155;i++){
g.setColor(getRandColor(140,200));
intx=random.nextInt(width);
inty=random.nextInt(HEIGHT);
intxl=random.nextInt(12);
intyl=random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
}
g.setFont(newFont("TimesNewRoman",Font.PLAIN,18));
StringsRand="";
for(inti=0;i intrand=random.nextInt(codeList.length()); StringstrRand=codeList.substring(rand,rand+1); sRand+=strRand; g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); g.drawString(strRand,13*i+6,16); } g.dispose(); try{ ImageIO.write(image,"JPEG",response.getOutputStream()); }catch(IOExceptione){ } returnsRand; } publicvoidsetBgColor(intr,intg,intb){ drawBgFlag=true; this.rBg=r; this.gBg=g; this.bBg=b; } 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); } } packageliuyanban; importjava.awt.Color; importjava.awt.Font; importjava.awt.Graphics; importjava.awt.image.BufferedImage; importjava.io.IOException; importjava.util.Random; importjavax.imageio.ImageIO; importjavax.servlet.http.HttpServletResponse; /** *生成验证码图片的类 */ publicclassRandImgCreater{ privatestaticfinalStringCODE_LIST="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"; privateHttpServletResponseresponse=null; privatestaticfinalintHEIGHT=20; privatestaticfinalintFONT_NUM=4; privateintwidth=0; privateintiNum=0; privateStringcodeList=""; privatebooleandrawBgFlag=false; privateintrBg=0; privateintgBg=0; privateintbBg=0; publicRandImgCreater(HttpServletResponseresponse){ this.response=response; this.width=13*FONT_NUM+12; this.iNum=FONT_NUM; this.codeList=CODE_LIST; } publicRandImgCreater(HttpServletResponseresponse,intiNum,StringcodeList){ this.response=response; this.width=13*iNum+12; this.iNum=iNum; this.codeList=codeList; } publicStringcreateRandImage(){ BufferedImageimage=newBufferedImage(width,HEIGHT, BufferedImage.TYPE_INT_RGB); Graphicsg=image.getGraphics(); Randomrandom=newRandom(); if(drawBgFlag){ g.setColor(newColor(rBg,gBg,bBg)); g.fillRect(0,0,width,HEIGHT); }else{ g.setColor(getRandColor(200,250)); g.fillRect(0,0,width,HEIGHT); for(inti=0;i<155;i++){ g.setColor(getRandColor(140,200)); intx=random.nextInt(width); inty=random.nextInt(HEIGHT); intxl=random.nextInt(12); intyl=random.nextInt(12); g.drawLine(x,y,x+xl,y+yl); } } g.setFont(newFont("TimesNewRoman",Font.PLAIN,18)); StringsRand=""; for(inti=0;i intrand=random.nextInt(codeList.length()); StringstrRand=codeList.substring(rand,rand+1); sRand+=strRand; g.setColor(newColor(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110))); g.drawString(strRand,13*i+6,16); } g.dispose(); try{ ImageIO.write(image,"JPEG",response.getOutputStream()); }catch(IOExceptione){ } returnsRand; } publicvoidsetBgColor(intr,intg,intb){ drawBgFlag=true; this.rBg=r; this.gBg=g; this.bBg=b; } 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); } } viewplaincopytoclipboardprint? ****************************************************** ******************************************************viewplaincopytoclipboardprint? 3.调用生成验证码图片: "img.jsp" 3.调用生成验证码图片: "img.jsp"viewplaincopytoclipboardprint? <% response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires",0); RandImgCreaterrc=newRandImgCreater(response); Stringrand=rc.createRandImage(); session.setAttribute("rand",rand); %> viewplaincopytoclipboardprint? viewplaincopytoclipboardprint? <%@pagecontentType="image/jpeg"import="liuyanban.*"%><%response.setHeader("Pragma","No-cache");response.setHeader("Cache-Control","no-cache");response.setDateHeader("Expires",0);RandImgCreaterrc=newRandImgCreater(response);Stringrand=rc.createRandImage();session.setAttribute("rand",rand);%><%@pagecontentType="image/jpeg"import="liuyanban.*"%> <% response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires",0); RandImgCreaterrc=newRandImgCreater(response); Stringrand=rc.createRandImage(); session.setAttribute("rand",rand); %> viewplaincopytoclipboardprint? ******************************************************************************************************** viewplaincopytoclipboardprint? viewplaincopytoclipboardprint? 4.测试
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Jsp 图片 验证 刷新
![提示](https://static.bdocx.com/images/bang_tan.gif)