




已阅读5页,还剩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.OutputStream;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.http.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 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生成 * 依赖jar包:patchca-0.5.0.jar * 项目网址:/p/patchca/ * * 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 ColorFactory 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 = null;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, HttpServletResponse response)throws ServletException, IOException response.setContentType(image/png);response.setHeader(cache, no-cache);HttpSession session = request.getSession(true);OutputStream outputStream = response.getOutputStream();/ 得到验证码对象,有验证码图片和验证码字符串Captcha captcha = configurableCaptchaService.getCaptcha();/ 取得验证码字符串放入SessionString validationCode = captcha.getChallenge();session.setAttribute(validationCode, validationCode);/ 取得验证码图片并输出BufferedImage bufferedImage = captcha.getImage();ImageIO.write(bufferedImage, png, outputStream);outputStream.flush();outputStream.close();/* * Servlet初始化方法 */public void init() throws ServletException configurableCaptchaService = new ConfigurableCaptchaService();/ 颜色创建工厂,使用一定范围内的随机色colorFactory = new RandomColorFactory();configurableCaptchaService.setColorFactory(colorFactory);/ 随机字体生成器fontFactory = new RandomFontFactory();fontFactory.setMaxSize(32);fontFactory.setMinSize(28);configurableCaptchaService.setFontFactory(fontFactory);/ 随机字符生成器,去除掉容易混淆的字母和数字,如o和0等wordFactory = new RandomWordFactory();wordFactory.setCharacters(abcdefghkmnpqstwxyz23456789);wordFactory.setMaxLength(5);wordFactory.setMinLength(4);configurableCaptchaService.setWordFactory(wordFactory);/ 自定义验证码图片背景MyCustomBackgroundFactory backgroundFactory = new MyCustomBackgroundFactory();configurableCaptchaService.setBackgroundFactory(backgroundFactory);/ 图片滤镜设置ConfigurableFilterFactory filterFactory = new ConfigurableFilterFactory();List filters = new ArrayList();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);/ 文字渲染器设置textRenderer = new BestFitTextRenderer();textRenderer.setBottomMargin(3);textRenderer.setTopMargin(3);configurableCaptchaService.setTextRenderer(textRenderer);/ 验证码图片的大小configurableCaptchaService.setWidth(82);configurableCaptchaService.setHeight(32);/* * 自定义验证码图片背景,主要画一些噪点和干扰线 */private class MyCustomBackgroundFactory 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.fillRect(0, 0, imgWidth, imgHeight);/ 画100个噪点(颜色及位置随机)for(int i = 0; i 100; i+) / 随机颜色int rInt = random.nextInt(255);int gInt = random.nextInt(255);int bInt = random.nextInt(255);graphics.setColor(new Color(rInt, gInt, bInt);/ 随机位置int xInt = random.nextInt(imgWidth - 3);int yInt = random.nextInt(imgHeight - 2);/ 随机旋转角度int sAngleInt = random.nextInt(360);int eAngleInt = random.nextInt(360);/ 随机大小int wInt = random.nextInt(6);int hInt = random.nextInt(6);graphics.fillArc(xInt, yInt, wInt, hInt, sAngleInt, eAngleInt);/ 画5条干扰线if (i % 20 = 0) int xInt2 = random.nextInt(imgWidth);int yInt2 = random.nextInt(imgHeight);graphics.drawLine(xInt, yInt, xInt2, yInt2);web.xml配置如下: validationCode com.ninemax.cul.servlet.ValidationCodeServlet validationCode /validationCodeServlet.png JSP引用(部分) img id=validationCode alt=验证码图片 title=验证码图片 src=/validationCodeServlet.png onclick=refreshCode(this) /换一张JS重新载入图片方法(参考):/* * 刷新验证码 * param i
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 系统化运营:夫妻二人共同投资茶馆的合伙协议
- 《涉及房产、股权、债务的夫妻离婚财产分割协议》
- 数字化云平台租赁电信机房服务器及维护服务合同
- 离异家庭子女抚养权、探望权及财产分割执行合同
- 智能家居平台合作合同续签及用户体验优化协议
- 2025年医疗器械国产化趋势下国际市场拓展与品牌建设研究报告
- 2025年制造业数据治理与工业互联网安全防护体系建设策略分析报告
- 汽车行业智能网联汽车2025年信息安全与隐私保护研究报告
- 中职专业笔试题库及答案
- 动物脱逃应急预案(3篇)
- 电子课件-《可编程序控制器及其应用(三菱-第三版)》-A04-1724-课题一-可编程序控制器基础知识
- 实验计划样表
- 三阶魔方入门教程课件
- 青少年亲社会行为量表
- 研发支出辅助账汇总表
- 计算机组装与维护完整版课件(全)
- 健康疗休养基本服务承诺书
- 聚合物混凝土定义、分类和性质Polymerconcrete
- 口袋妖怪(宠物小精灵)1至649图鉴
- Q∕GDW 11612.2-2018 低压电力线高速载波通信互联互通技术规范 第2部分:技术要求
- 计算机网络技术ppt课件(完整版)
评论
0/150
提交评论