Java Web:使用Servlet生成网页随机图片验证码

最近在学习Java Web开发,做了一个生成网页随机图片验证码的例子,在此记录。

一、新建Servlet项目:

在MyEclipse中新建Servlet项目,一步步操作就OK,在此不再赘述。建好之后文件目录树如下图:

二、源代码实现:

(1)java代码:

package com.zdt.identity;import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.PrintWriter;import java.util.Random;import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.sun.image.codec.jpeg.JPEGCodec;import com.sun.image.codec.jpeg.JPEGImageEncoder;public class Servlet1 extends HttpServlet {//随机字符集合public static final char[] CHARS = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};//随机数public static Random random = new Random();//获取6位随机数public static String getRandomString() {//字符串缓存StringBuffer strBuffer = new StringBuffer();//循环从字符集中随机取出6个字符for (int i = 0; i < 6; i++) {strBuffer.append(CHARS[random.nextInt(CHARS.length)]);}return strBuffer.toString();}//获取随机颜色public static Color getRandomColor() {return new Color(random.nextInt(255), random.nextInt(255),random.nextInt(255));}//获取某一颜色的反色public static Color getReverseColor(Color c) {return new Color(255 – c.getRed(), 255 – c.getGreen(),255 – c.getBlue());}/** * Constructor of the object. */public Servlet1() {super();}/** * Destruction of the servlet. <br> */public void destroy() {super.destroy(); // Just puts "destroy" string in log// Put your code here}/** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {//设置输出的类型,此处为图片response.setContentType("image/jpeg");//获取随机字符串String randomString = getRandomString();//把随机字符串绑定到当前会话request.getSession(true).setAttribute("randomString", randomString);int width = 100;//图片宽度int height = 30;//图片高度//获取一种随机颜色Color color = getRandomColor();//获取上述颜色的反色Color reverseColor = getReverseColor(color);//根据宽度和高度,创建一个彩色图片BufferedImage bi = new BufferedImage(width, height,BufferedImage.TYPE_INT_BGR);//获取绘图对象Graphics2D graphics2d = bi.createGraphics();//设置字体graphics2d.setFont(new Font(Font.SANS_SERIF,Font.BOLD, 16));//设置颜色graphics2d.setColor(color);//绘制背景graphics2d.fillRect(0, 0, width, height);//设置背景颜色,与字符颜色相反graphics2d.setColor(reverseColor);//绘制随机字符graphics2d.drawString(randomString, 18, 20);//绘制随机噪音点,最多绘制100个for (int i = 0, n = random.nextInt(100); i < n; i++) {graphics2d.drawRect(random.nextInt(width),random.nextInt(height), 1, 1);}//响应的输出流ServletOutputStream servletOutputStream = response.getOutputStream();//转换成JPEG格式JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(servletOutputStream);//对图片编码encoder.encode(bi);//输出到客户端servletOutputStream.flush();}/** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {}/** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */public void init() throws ServletException {// Put your code here}} 注意:在导入JPEG图片处理包JPEGCodec和JPEGImageEncoder时可能会报错,解决方案见:

(2)写web.xml配置文件,主要代码如下:

梦想,并不奢侈,只要勇敢地迈出第一步。

Java Web:使用Servlet生成网页随机图片验证码

相关文章:

你感兴趣的文章:

标签云: