Jsp-图片验证码及刷新_第1页
Jsp-图片验证码及刷新_第2页
Jsp-图片验证码及刷新_第3页
Jsp-图片验证码及刷新_第4页
Jsp-图片验证码及刷新_第5页
已阅读5页,还剩5页未读, 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、1.图片验证码的实现主要的技术点是如何生成一个图片。生成图片可以使用java.awt包下的类来实现。我们先写一个简单的生成图片的程序HelloImage.java。以下是代码部分。(是我在做留言板项目时的部分代码)HelloImage .javaview plaincopy to clipboardprint?package liuyanban;import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOExcept

2、ion;import javax.imageio.ImageIO;/* 生成图片*/public class HelloImage public static void main(String args)BufferedImage image = new BufferedImage(80, 50,BufferedImage.TYPE_INT_RGB);/建立BufferedImage对象。指定图片的长度宽度和色彩。Graphics g = image.getGraphics();/取得Graphics对象,用来绘制图片。g.setColor(new Color(255,255,255);/绘制

3、图片背景和文字。g.fillRect(0, 0, 80, 25);g.setColor(new Color(0,0,0);g.drawString(HelloImage,6,16);g.dispose();/释放Graphics对象所占用的资源。tryImageIO.write(image, jpeg, new File(C:helloImage.jpeg);/通过ImageIO对象的write静态方法将图片输出。catch(IOException e)e.printStackTrace();package liuyanban;import java.awt.Color;import java

4、.awt.Graphics;import java.awt.image.BufferedImage;import java.io.File;import java.io.IOException;import javax.imageio.ImageIO;/* 生成图片*/public class HelloImage public static void main(String args)BufferedImage image = new BufferedImage(80, 50,BufferedImage.TYPE_INT_RGB);/建立BufferedImage对象。指定图片的长度宽度和色

5、彩。Graphics g = image.getGraphics();/取得Graphics对象,用来绘制图片。g.setColor(new Color(255,255,255);/绘制图片背景和文字。g.fillRect(0, 0, 80, 25);g.setColor(new Color(0,0,0);g.drawString(HelloImage,6,16);g.dispose();/释放Graphics对象所占用的资源。tryImageIO.write(image, jpeg, new File(C:helloImage.jpeg);/通过ImageIO对象的write静态方法将图片输

6、出。catch(IOException e)e.printStackTrace();view plaincopy to clipboardprint?*viewclipboardprint?2.生成验证码图片的类2.生成验证码图片的类view plaincopy to clipboardprint?package liuyanban;plaincopy toimport java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.I

7、OException;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletResponse;/* 生成验证码图片的类*/public class RandImgCreater private static final String CODE_LIST ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890;private HttpServletResponse response = null;pri

8、vate static final int HEIGHT = 20;private static final int FONT_NUM = 4;private int width = 0;private int iNum = 0;private String codeList = ;private boolean drawBgFlag = false;private int rBg = 0; =private int gBg = 0;private int bBg = 0;public RandImgCreater(HttpServletResponse response) this.resp

9、onse = response;this.width = 13 * FONT_NUM + 12;this.iNum = FONT_NUM;this.codeList = CODE_LIST;public RandImgCreater(HttpServletResponse response,int iNum,String codeList) this.response = response;this.width = 13 * iNum + 12;this.iNum = iNum;this.codeList = codeList;public String createRandImage()Bu

10、fferedImage image = new BufferedImage(width, HEIGHT,BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();Random random = new Random();if ( drawBgFlag )g.setColor(new Color(rBg,gBg,bBg);g.fillRect(0, 0, width, HEIGHT);elseg.setColor(getRandColor(200, 250);g.fillRect(0, 0, width, HEIGHT);for (

11、int i = 0; i 155; i+) g.setColor(getRandColor(140, 200);int x = random.nextInt(width);int y = random.nextInt(HEIGHT);int xl = random.nextInt(12);int yl = random.nextInt(12);g.drawLine(x, y, x + xl, y + yl);g.setFont(new Font(Times New Roman, Font.PLAIN, 18);String sRand=;for (int i=0;i 255)fc = 255;

12、if (bc 255)bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);package liuyanban;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOExce

13、ption;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.http.HttpServletResponse;/* 生成验证码图片的类*/public class RandImgCreater private static final String CODE_LIST ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890;private HttpServletResponse response = null;private

14、static final int HEIGHT = 20;private static final int FONT_NUM = 4;private int width = 0;private int iNum = 0; =private String codeList = ;private boolean drawBgFlag = false;private int rBg = 0;private int gBg = 0;private int bBg = 0;public RandImgCreater(HttpServletResponse response) this.response

15、= response;this.width = 13 * FONT_NUM + 12;this.iNum = FONT_NUM;this.codeList = CODE_LIST;public RandImgCreater(HttpServletResponse response,int iNum,String codeList) this.response = response;this.width = 13 * iNum + 12;this.iNum = iNum;this.codeList = codeList;public String createRandImage()Buffere

16、dImage image = new BufferedImage(width, HEIGHT,BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();Random random = new Random();if ( drawBgFlag )g.setColor(new Color(rBg,gBg,bBg);g.fillRect(0, 0, width, HEIGHT);elseg.setColor(getRandColor(200, 250);g.fillRect(0, 0, width, HEIGHT);for (int i

17、 = 0; i 155; i+) g.setColor(getRandColor(140, 200);int x = random.nextInt(width);int y = random.nextInt(HEIGHT);int xl = random.nextInt(12);int yl = random.nextInt(12);g.drawLine(x, y, x + xl, y + yl);g.setFont(new Font(Times New Roman, Font.PLAIN, 18);String sRand=;for (int i=0;i 255)fc = 255;if (b

18、c 255)bc = 255;int r = fc + random.nextInt(bc - fc);int g = fc + random.nextInt(bc - fc);int b = fc + random.nextInt(bc - fc);return new Color(r, g, b);view plaincopy to clipboardprint?*viewclipboardprint?3.调用生成验证码图片:img.jsp3.调用生成验证码图片:img.jspview plaincopy to clipboardprint?* 4.测试,验证,刷新: name=code% page plaincopy toview plaincopy to clipboardprint?response.setHeader(Pragma,No-cache); name=code% page * 4.测试,验证,刷新: view plaincopy to clipboardprint? view plaincopy to clipboardprint?* *view plaincopy to clipboardprint?view plaincopy to clipboardprint?4.测试,验证,刷新: 4.测试,验证,刷新:view plainc

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论