Jsp图片验证码及刷新.docx

上传人:b****3 文档编号:12880778 上传时间:2023-04-22 格式:DOCX 页数:19 大小:17.03KB
下载 相关 举报
Jsp图片验证码及刷新.docx_第1页
第1页 / 共19页
Jsp图片验证码及刷新.docx_第2页
第2页 / 共19页
Jsp图片验证码及刷新.docx_第3页
第3页 / 共19页
Jsp图片验证码及刷新.docx_第4页
第4页 / 共19页
Jsp图片验证码及刷新.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

Jsp图片验证码及刷新.docx

《Jsp图片验证码及刷新.docx》由会员分享,可在线阅读,更多相关《Jsp图片验证码及刷新.docx(19页珍藏版)》请在冰豆网上搜索。

Jsp图片验证码及刷新.docx

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?

<%@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);

%>

****************************************************

4.测试,验证,刷新:

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);%>****************************************************4.测试,验证,刷新:

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.测试

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 医药卫生 > 基础医学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1