java验证码:Patchca(简单而功能强大).doc_第1页
java验证码:Patchca(简单而功能强大).doc_第2页
java验证码:Patchca(简单而功能强大).doc_第3页
java验证码:Patchca(简单而功能强大).doc_第4页
免费预览已结束,剩余1页可下载查看

下载本文档

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

文档简介

Patchca简单而功能强大Jar包下载:/files/patchca-0.5.0.zip源代码:/svn/trunk/一:定义一个servlet生成图片(先引入jar包)(1) :配置xmlpatchcacom.ssh.servlet.CaptchaServletpatchca/patchca.png(2) :servlet(自定义CaptchaService)public class CaptchaServlet extends HttpServlet private static final long serialVersionUID = 4968328161261528097L;private static MyCaptchaService cs = null;Overridepublic void init() throws ServletException super.init();/可直接使用ConfigurableCaptchaService,然后修改配置cs = new MyCaptchaService();Overridepublic void destroy() cs = null;super.destroy();public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(image/png);response.setHeader(cache, no-cache);HttpSession session = request.getSession(true);OutputStream os = response.getOutputStream();String patchca= EncoderHelper.getChallangeAndWriteImage(cs, png, os);session.setAttribute(PATCHCA, patchca);os.flush();os.close();public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException this.doGet(request, response);private class MyCaptchaService extends AbstractCaptchaService public MyCaptchaService() /文本内容wordFactory = new MyWordFactory();/字体fontFactory = new RandomFontFactory();/效果textRenderer = new BestFitTextRenderer();/背景backgroundFactory = new SingleColorBackgroundFactory();/字体颜色colorFactory = new SingleColorFactory(new Color(25, 60, 170);/样式(曲线波纹加干扰线)filterFactory = new CurvesRippleFilterFactory(colorFactory);/图片长宽width = 150;height = 50;private class MyWordFactory extends RandomWordFactory public MyWordFactory() /文本范围和长度characters = absdekmnowx23456789;minLength = 5;maxLength = 4;(3) 包中提供的配置:一:ConfigurableCaptchaService:FontFactory 字体默认:RandomFontFactory(Verdana,Tahoma,minSize、maxSize=45)WordFactory 单词范围和长度默认:RandomWordFactory 单词6-6其他:AdaptiveRandomWordFactory 自适应(mw字母宽度加长)ColorFactory默认:SingleColorFactory 黑色BackgroundFactory默认:SingleColorBackgroundFactory 白色TextRenderer 文本渲染默认:BestFitTextRendererFilterFactory 样式默认:CurvesRippleFilterFactory(曲线波纹带干扰线,干扰线颜色为当前colorFactory)width/height 图片长宽(默认160*70)使用:public class CaptchaServlet extends HttpServlet private static final long serialVersionUID = 4968328161261528097L;private static ConfigurableCaptchaService cs = null;private static ColorFactory cf = null;private static RandomWordFactory wf = null;private static Random r = new Random();private static CurvesRippleFilterFactory crff = null;private static MarbleRippleFilterFactory mrff = null;private static DoubleRippleFilterFactory drff = null;private static WobbleRippleFilterFactory wrff = null;private static DiffuseRippleFilterFactory dirff = null;Overridepublic void init() throws ServletException super.init();cs = new ConfigurableCaptchaService();cf = new SingleColorFactory(new Color(25, 60, 170);wf = new RandomWordFactory();crff = new CurvesRippleFilterFactory(cs.getColorFactory();drff = new DoubleRippleFilterFactory();wrff = new WobbleRippleFilterFactory();dirff = new DiffuseRippleFilterFactory();mrff = new MarbleRippleFilterFactory();cs.setWordFactory(wf);cs.setColorFactory(cf);cs.setWidth(120);cs.setHeight(50);Overridepublic void destroy() wf = null;cf = null;cs = null;super.destroy();public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException response.setContentType(image/png);response.setHeader(cache, no-cache);wf.setMaxLength(5);wf.setMinLength(3);HttpSession session = request.getSession(true);OutputStream os = response.getOutputStream();/随机产生5种效果(filter)switch (r.nextInt(5) case 0:cs.setFilterFactory(crff);break;case 1:cs.setFilterFactory(mrff);break;case 2:cs.setFilterFactory(drff);break;case 3:cs.setFilterFactory(wrff);break;case 4:cs.setFilterFactory(dirff);break;String patchca= EncoderHelper.getChallangeAndWriteImage(cs, png, os);session.setAttribute(PATCHCA, patchca);os.flush();os.close();二:需要自己构造SimpleCaptchaService(int width, int height, Color textColor, Color backgroundColor, int fontSize, StringfontNames, FilterFactory ff) backgroundFactory = new SingleColorBackgroundFactory(backgroundColor);wordFactory = new AdaptiveRandomWordFactory();fontFactory = new RandomFontFactory(fontNames);textRenderer = new BestFitTextRenderer();colorFactory = new SingleColorFactory(textColor);filterFactory = ff;this.width = width;this.height = height;(4) FilterFactory样式二:jsp页面访问servlet!- patchca.png和servlet中的对应 -验证码:三:jsp页面取出session中的验证码%/PATCHCA和servlet中定义的session保存位置对应String c = (String)session.getAtt

温馨提示

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

评论

0/150

提交评论