1、用之前要导入 patchca jar 包,可能会与 tomcat 的 lib 目录下的 jar 包发生冲突,一切准备好之后在使用下面的代码(导入 jar 包后,先发布其他的项目试试,没问题的发继续)package com.ninemax.cul.servlet;import java.awt.Color;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.awt.image.BufferedImageOp;import java.io.IOException;import java.io.Output
2、Stream;import java.util.ArrayList;import java.util.List;import java.util.Random;import javax.imageio.ImageIO;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.htt
3、p.HttpSession;import org.patchca.background.BackgroundFactory;import org.patchca.color.ColorFactory;import org.patchca.color.RandomColorFactory;import org.patchca.filter.ConfigurableFilterFactory;import org.patchca.filter.library.AbstractImageOp;import org.patchca.filter.library.WobbleImageOp;import
4、 org.patchca.font.RandomFontFactory;import org.patchca.service.Captcha;import org.patchca.service.ConfigurableCaptchaService;import org.patchca.text.renderer.BestFitTextRenderer;import org.patchca.text.renderer.TextRenderer;import org.patchca.word.RandomWordFactory;/* * 验证码生成类 * * 使用开源验证码项目 patchca
5、生成 * 依赖 jar 包:patchca-0.5.0.jar * 项目网址: https:/ * * author zyh* version 1.00 2012-7-12 New */public class ValidationCodeServlet extends HttpServlet private static final long serialVersionUID = 5126616339795936447L;private ConfigurableCaptchaService configurableCaptchaService = null;private ColorFact
6、ory colorFactory = null;private RandomFontFactory fontFactory = null;private RandomWordFactory wordFactory = null;private TextRenderer textRenderer = null;public ValidationCodeServlet() super();/* Servlet 销毁方法, 负责销毁所使用资源. */public void destroy() wordFactory = null;colorFactory = null;fontFactory = n
7、ull;textRenderer = null;configurableCaptchaService = null;super.destroy(); / Just puts “destroy“ string in logpublic void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException doPost(request, response);public void doPost(HttpServletRequest request, HttpS
8、ervletResponse response)throws ServletException, IOException response.setContentType(“image/png“);response.setHeader(“cache“, “no-cache“);HttpSession session = request.getSession(true);OutputStream outputStream = response.getOutputStream();/ 得到验证码对象, 有验证码图片和验证码字符串Captcha captcha = configurableCaptch
9、aService.getCaptcha();/ 取得验证码字符串放入 SessionString validationCode = captcha.getChallenge();session.setAttribute(“validationCode“, validationCode);/ 取得验证码图片并输出BufferedImage bufferedImage = captcha.getImage();ImageIO.write(bufferedImage, “png“, outputStream);outputStream.flush();outputStream.close();/*
10、Servlet 初始化方法*/public void init() throws ServletException configurableCaptchaService = new ConfigurableCaptchaService();/ 颜色创建工厂,使用一定范围内的随机色colorFactory = new RandomColorFactory();configurableCaptchaService.setColorFactory(colorFactory);/ 随机字体生成器fontFactory = new RandomFontFactory();fontFactory.setM
11、axSize(32);fontFactory.setMinSize(28);configurableCaptchaService.setFontFactory(fontFactory);/ 随机字符生成器, 去除掉容易混淆的字母和数字 ,如 o 和 0 等wordFactory = new RandomWordFactory();wordFactory.setCharacters(“abcdefghkmnpqstwxyz23456789“);wordFactory.setMaxLength(5);wordFactory.setMinLength(4);configurableCaptchaSe
12、rvice.setWordFactory(wordFactory);/ 自定义验证码图片背景MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();configurableCaptchaService.setBackgroundFactory(backgroundFactory);/ 图片滤镜设置ConfigurableFilterFactory filterFactory = new ConfigurableFilterFactory();List filters = new ArrayLis
13、t();WobbleImageOp wobbleImageOp = new WobbleImageOp();wobbleImageOp.setEdgeMode(AbstractImageOp.EDGE_MIRROR);wobbleImageOp.setxAmplitude(2.0);wobbleImageOp.setyAmplitude(1.0);filters.add(wobbleImageOp);filterFactory.setFilters(filters);configurableCaptchaService.setFilterFactory(filterFactory);/ 文字渲
14、染器设置textRenderer = new BestFitTextRenderer();textRenderer.setBottomMargin(3);textRenderer.setTopMargin(3);configurableCaptchaService.setTextRenderer(textRenderer);/ 验证码图片的大小configurableCaptchaService.setWidth(82);configurableCaptchaService.setHeight(32);/* 自定义验证码图片背景, 主要画一些噪点和干扰线*/private class MyCu
15、stomBackgroundFactory implements BackgroundFactory private Random random = new Random();public void fillBackground(BufferedImage image) Graphics graphics = image.getGraphics();/ 验证码图片的宽高int imgWidth = image.getWidth();int imgHeight = image.getHeight();/ 填充为白色背景graphics.setColor(Color.WHITE);graphics
16、.fillRect(0, 0, imgWidth, imgHeight);/ 画 100 个噪点 (颜色及位置随机)for(int i = 0; i validationCodecom.ninemax.cul.servlet.ValidationCodeServletvalidationCode/validationCodeServlet.pngJSP 引用(部分)/validationCodeServlet.png“ onclick=“refreshCode(this)“ /换一张JS 重新载入图片方法( 参考):/* 刷新验证码* param imgObj 验证码 Img 元素*/function refreshCode(imgObj) if (!imgObj) imgObj = document.getElementById(“validationCode“);var index = imgObj.src.indexOf(“?“);if(index != -1) var url = imgObj.src.substring(0,index + 1);imgObj.src = url + Math.random(); else imgObj.src = imgObj.src + “?“ + Math.random();