当前位置: 移动技术网 > IT编程>软件设计>面向对象 > 项目模块--1.实现验证码功能

项目模块--1.实现验证码功能

2020年03月17日  | 移动技术网IT编程  | 我要评论

简介:

java web项目中,在后端随机生成一个验证码,绘制成图像,并在图像上添加两条干扰线,发送到浏览器,供用户使用。

本片博文内容包括,功能实现的逻辑步骤,java实现代码,生成的验证码图片展示。

步骤一:生成一个包含四个字符的字符串

使用一个数组char[]+一个random对象实现该功能。代码如下:


private char code[]={
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'l', 'm',
'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2',
'3', '4', '5', '6', '7', '8', '9' };
random rd = new random();

string result = "";

for(int i = 0;i < 4;i++ ){
  result+=code[rd.nextint[code.length]];
}
 


步骤二:绘制图片

使用一个bufferdimage对象,graphics对象,font对象

bufferedimage类:一个图像类,它生成的图像在内存中占有一个缓存区。

使用方法有两种:

1.读取一个已经存在的图片文件到bufferedimage对象的缓存区中,然后对该图片文件进行操作。

  bufferedimage b = imageio.read(new fileoutputstream(file f));

2.创建一个bufferedimage对象,然后通过graphics对象对其进行绘制。

  bufferedimage b = new bufferedimage(int width,int height,indexcolormodel cn);  //设定图片的宽度,高度,颜色设定模式

  graphics g = b.getgraphics();                        //得到图片绘制器

3.graphics对象常用方法

  g.setcolor(color color);

  g.fillrect(int x,int y,int width,int heigh)                    //两个一组,为图片的指定位置设置颜色,以图片的左下角为原点,(x,y)为绘制图片的左下

                                      //角坐标,绘制面积为width,height。

  g.drawstring(string s,int x,int y)                      //在(x,y)坐标系下绘制字符串,坐标系原点为bufferedimage的左下角

  g.drawline(int x1,int y1,int x2,int y2)                    //在两点之间绘制一条直线,颜色使用最近的g.setcolor()设定。

  g.dispose()                               //释放正在使用的系统资源

 1 bufferdimage bufferedimage = new     bufferedimage(50,30,bufferedimage.type_int_bgr);
 2 graphics graphics = bufferedimage.getgraphics();
 3 font font = new font("arial",font.truetype_font,18);
 4 //设置背景颜色,并填充
 5 graphics.setcolor(new color(rd.nextint(55)+200,rd.nextint(55)+20,rd.nextint(55)+200));
 6 graphics.fillrect(0,0,width,heigh);   //设定要填充的矩形的坐标(x,y),矩形的宽度和高度
 7 //边框颜色
 8 graphics.setcolor(color.black);
 9 graphics.fillrect(0,0,width-1,heigh-1);
10 //设置字体
11 graphics.setfont(font);        
12 for (int i = 0; i <result.length() ; i++) {
13             g.setcolor(new color(rd.nextint(200),rd.nextint(200),rd.nextint(200)));
14             g.drawstring(result.charat(i)+"",12*i+1,16);  //需要绘制的字符串,绘制的位置。
15         }
16 for (int i = 0; i < 2 ; i++) {
17             g.setcolor(new color(rd.nextint(200),rd.nextint(200),rd.nextint(200)));
18             int x1 = rd.nextint(width);
19             int x2 = rd.nextint(width);
20             int y1 = rd.nextint(heigh);
21             int y2 = rd.nextint(heigh);
22             g.drawline(x1,y1,x2,y2);
23         }

步骤三:将图片发送到浏览器端,存放此次产生的验证码中的字符串

设置浏览器端不能缓存验证码图片

pragma域名:用来包含实现特定的指令,pragma:no-cache(不允许缓存响应或请求报文)

cache-controller域名:在http:1.1中使用,指定缓存规则。

response.setheader("pragma","no-cache");   //通知浏览器不要使用缓存功能
        response.setheader("cache-control","no-cache"); //通知浏览器不要缓存
        response.setdateheader("expires",0);

设置响应的媒体类型

response.setcontenttype("image/jpeg");

将验证码中的字符串发送到session中,以便验证时从session中获取

session.setattribute("code",result);

将验证码图片发送给浏览器端

1 try{
2      outputstream os = reponse.getoutputstream();
3      imageio.write(image,"jpeg",os);  
4 }catch(ioexception e){
5      e.printstacktrace();  
6 }

imageio类:

  图像读写工具类,提供静态方法来将任意格式的图片读取到内存中或写入到文件中。

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网