java验证码.doc_第1页
java验证码.doc_第2页
java验证码.doc_第3页
java验证码.doc_第4页
java验证码.doc_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

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

文档简介

生成图形验证码的ActionJava代码 1. packagecom.wjt276.co.web.actions; 2. 3. importjava.io.ByteArrayInputStream; 4. importjava.io.ByteArrayOutputStream; 5. importjava.io.IOException; 6. 7. importcom.wjt276.co.managers.ValidateImageManager; 8. 9. 10. SuppressWarnings(serial) 11. publicclassValidateImgActionextendsBaseAction 12. 13. /* 14. *用于保存已经生成的图形验证码输入流对象,以便在前台显示图形 15. */16. privateByteArrayInputStreaminputStream; 17. 18. /* 19. *图形验证码创建接口 20. */21. privateValidateImageManagervalidateImageManager; 22. 23. /* 24. *图形验证码图形的宽度 25. */26. privateintwidth; 27. /* 28. *图形验证码图形的高度 29. */30. privateintheight; 31. /* 32. *图形验证码图形上字体大小 33. */34. privateintfontSize; 35. /* 36. *生成验证码的位数 37. */38. privateintcodeLength; 39. 40. /* 41. *图形验证码的图形类型 42. */43. privateStringcontentType; 44. 45. 46. 47. publicStringexecute()throwsException 48. /保存已经生成好的验证码 49. Key.validateCode=createInputStream(ValidateImageManager.Disturb_Type_Complex); 50. returnSUCCESS; 51. 52. 53. /* 54. *在Action层创建图形验证码,并将创建好的图形字节流保存到inputStream对象中,并返回验证码 55. *paramdisturbType绘制干扰线的类型; 56. *0:不绘制干扰线; 57. *1:绘制单一色调的干扰线; 58. *2:绘制复杂的干扰线 59. *return返回已经生成好的验证码字符 60. *throwsIOException 61. */62. protectedStringcreateInputStream(intdisturbType)throwsIOException 63. ByteArrayOutputStreambos=newByteArrayOutputStream(); 64. StringvalidateCode=null; 65. validateCode=validateImageManager.createValidateCode(disturbType, 66. this.getFontSize(), 67. bos, 68. this.getWidth(), 69. this.getHeight(), 70. getText(System.validateCode,ValidateImageManager.Default_ValidateCode), 71. this.getCodeLength(); 72. inputStream=newByteArrayInputStream(bos.toByteArray(); 73. bos.close(); 74. returnvalidateCode; 75. 76. 77. 78. /-以下为getXXX/setXXX方法- 79. 80. 81. 82. publicByteArrayInputStreamgetInputStream() 83. returninputStream; 84. 85. 86. publicvoidsetInputStream(ByteArrayInputStreaminputStream) 87. this.inputStream=inputStream; 88. 89. 90. publicValidateImageManagergetValidateImageManager() 91. returnvalidateImageManager; 92. 93. 94. publicvoidsetValidateImageManager(ValidateImageManagervalidateImageManager) 95. this.validateImageManager=validateImageManager; 96. 97. 98. publicStringgetContentType() 99. if(null=this.contentType|.equals(this.contentType.trim() 100. returnKey.DEFAULT_VALIDATEIMG_CONTENT_TYPE; 101. 102. returncontentType; 103. 104. 105. publicvoidsetContentType(StringcontentType) 106. this.contentType=contentType; 107. 108. 109. publicintgetWidth() 110. if(this.widthKey.DEFAULT_VALIDATEIMG_WIDTH) 111. returnKey.DEFAULT_VALIDATEIMG_WIDTH; 112. 113. returnwidth; 114. 115. 116. publicvoidsetWidth(intwidth) 117. this.width=width; 118. 119. 120. publicintgetHeight() 121. if(this.heightKey.DEFAULT_VALIDATEIMG_HEIGHT) 122. returnKey.DEFAULT_VALIDATEIMG_HEIGHT; 123. 124. returnheight; 125. 126. 127. publicvoidsetHeight(intheight) 128. this.height=height; 129. 130. 131. publicintgetFontSize() 132. if(this.fontSizeKey.DEFAULT_VALIDATEIMG_FONT_SIZE) 133. returnKey.DEFAULT_VALIDATEIMG_FONT_SIZE; 134. 135. returnfontSize; 136. 137. 138. publicvoidsetFontSize(intfontSize) 139. this.fontSize=fontSize; 140. 141. 142. publicintgetCodeLength() 143. if(this.codeLengthKey.DEFAULT_VALIDATEIMG_CODE_LENGTH) 144. returnKey.DEFAULT_VALIDATEIMG_CODE_LENGTH; 145. 146. returncodeLength; 147. 148. 149. publicvoidsetCodeLength(intcodeLength) 150. this.codeLength=codeLength; 151. 152. 153. package com.wjt276.co.web.actions;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.IOException;import com.wjt276.co.managers.ValidateImageManager;SuppressWarnings(serial)public class ValidateImgAction extends BaseAction /* * 用于保存已经生成的图形验证码输入流对象,以便在前台显示图形 */private ByteArrayInputStream inputStream;/* * 图形验证码创建接口 */private ValidateImageManager validateImageManager;/* * 图形验证码图形的宽度 */private int width;/* * 图形验证码图形的高度 */private int height;/* * 图形验证码图形上字体大小 */private int fontSize;/* * 生成验证码的位数 */private int codeLength;/* * 图形验证码的图形类型 */private String contentType;public String execute() throws Exception/保存已经生成好的验证码Key.validateCode = createInputStream(ValidateImageManager.Disturb_Type_Complex);return SUCCESS;/* * 在Action层创建图形验证码,并将创建好的图形字节流保存到inputStream对象中,并返回验证码 * param disturbType 绘制干扰线的类型; * 0:不绘制干扰线; * 1:绘制单一色调的干扰线; * 2:绘制复杂的干扰线 * return 返回已经生成好的验证码字符 * throws IOException */protected String createInputStream(int disturbType) throws IOException ByteArrayOutputStream bos = new ByteArrayOutputStream();String validateCode = null;validateCode = validateImageManager.createValidateCode(disturbType,this.getFontSize(), bos, this.getWidth(), this.getHeight(), getText(System.validateCode,ValidateImageManager.Default_ValidateCode), this.getCodeLength();inputStream = new ByteArrayInputStream(bos.toByteArray();bos.close();return validateCode;/-以下为getXXX/setXXX方法-public ByteArrayInputStream getInputStream() return inputStream;public void setInputStream(ByteArrayInputStream inputStream) this.inputStream = inputStream;public ValidateImageManager getValidateImageManager() return validateImageManager;public void setValidateImageManager(ValidateImageManager validateImageManager) this.validateImageManager = validateImageManager;public String getContentType() if(null = this.contentType | .equals(this.contentType.trim()return Key.DEFAULT_VALIDATEIMG_CONTENT_TYPE;return contentType;public void setContentType(String contentType) this.contentType = contentType;public int getWidth() if(this.width Key.DEFAULT_VALIDATEIMG_WIDTH)return Key.DEFAULT_VALIDATEIMG_WIDTH;return width;public void setWidth(int width) this.width = width;public int getHeight() if(this.height Key.DEFAULT_VALIDATEIMG_HEIGHT)return Key.DEFAULT_VALIDATEIMG_HEIGHT;return height;public void setHeight(int height) this.height = height;public int getFontSize() if(this.fontSize Key.DEFAULT_VALIDATEIMG_FONT_SIZE)return Key.DEFAULT_VALIDATEIMG_FONT_SIZE;return fontSize;public void setFontSize(int fontSize) this.fontSize = fontSize;public int getCodeLength() if(this.codeLength Key.DEFAULT_VALIDATEIMG_CODE_LENGTH)return Key.DEFAULT_VALIDATEIMG_CODE_LENGTH;return codeLength;public void setCodeLength(int codeLength) this.codeLength = codeLength;二、生成图形验证码的接口ValidateImageManagerJava代码 1. packagecom.wjt276.co.managers; 2. 3. importjava.io.ByteArrayOutputStream; 4. 5. publicinterfaceValidateImageManager 6. /* 7. *默认验证字符串 8. */9. publicstaticfinalStringDefault_ValidateCode=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789; 10. 11. /* 12. *默认绘制干扰线的类型(不绘制干扰线) 13. */14. publicstaticfinalintDisturb_Type_Default=0; 15. 16. /* 17. *绘制单一色调的干扰线 18. */19. publicstaticfinalintDisturb_Type_Simple=1; 20. 21. /* 22. *绘制复杂的干扰线 23. */24. publicstaticfinalintDisturb_Type_Complex=2; 25. 26. /* 27. *生成验证图片并返回验证码 28. * 29. *paramdisturbType绘制干扰线的类型; 30. *0:不绘制干扰线; 31. *1:绘制单一色调的干扰线; 32. *2:绘制复杂的干扰线 33. *paramfontSize图形上验证码字体的大小 34. *parambos字节流,用于返回生成的图形验证码字节流,并显示于前台 35. *paramwidth图形的宽度 36. *paramheight图形的高度 37. *paramvalidateCode默认的验证码编码 38. *paramcodeLength验证码字符长度 39. *return返回生成好的验证码字符 40. */41. publicabstractStringcreateValidateCode(intdisturbType,intfontSize, 42. ByteArrayOutputStreambos,intwidth,intheight, 43. StringvalidateCode,intcodeLength); 44. package com.wjt276.co.managers;import java.io.ByteArrayOutputStream;public interface ValidateImageManager /* * 默认验证字符串 */public static final String Default_ValidateCode = ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789;/* * 默认绘制干扰线的类型(不绘制干扰线) */public static final int Disturb_Type_Default = 0;/* * 绘制单一色调的干扰线 */public static final int Disturb_Type_Simple = 1;/* * 绘制复杂的干扰线 */public static final int Disturb_Type_Complex = 2;/* * 生成验证图片并返回验证码 * * param disturbType 绘制干扰线的类型; * 0:不绘制干扰线; * 1:绘制单一色调的干扰线; * 2:绘制复杂的干扰线 * param fontSize 图形上验证码字体的大小 * param bos 字节流,用于返回生成的图形验证码字节流,并显示于前台 * param width 图形的宽度 * param height 图形的高度 * param validateCode 默认的验证码编码 * param codeLength 验证码字符长度 * return 返回生成好的验证码字符 */public abstract String createValidateCode(int disturbType, int fontSize,ByteArrayOutputStream bos, int width, int height,String validateCode, int codeLength);一、生成图形验证码的实现ValidateImageManagerImplJava代码 1. packagecom.wjt276.co.managers.impl; 2. 3. importjava.awt.Color; 4. importjava.awt.Font; 5. importjava.awt.Graphics; 6. importjava.awt.image.BufferedImage; 7. importjava.io.ByteArrayOutputStream; 8. importjava.io.IOException; 9. importjava.util.Random; 10. 11. importjavax.imageio.ImageIO; 12. importjavax.imageio.stream.ImageOutputStream; 13. 14. importcom.wjt276.co.managers.ValidateImageManager; 15. 16. publicclassValidateImageManagerImplextendsAbstractManagerimplements17. ValidateImageManager 18. 19. /* 20. *生成验证图片并返回验证码 21. * 22. *paramdisturbType绘制干扰线的类型; 23. *0:不绘制干扰线;1:绘制单一色调的干扰线;2:绘制复杂的干扰线 24. *paramfontSize图形上验证码字体的大小 25. *parambos字节流,用于返回生成的图形验证码字节流,并显示于前台 26. *paramwidth图形的宽度 27. *paramheight图形的高度 28. *paramvalidateCode默认的验证码编码 29. *paramcodeLength验证码字符长度 30. *return返回生成好的验证码字符 31. */32. publicStringcreateValidateCode(intdisturbType, 33. intfontSize, 34. ByteArrayOutputStreambos, 35. intwidth, 36. intheight, 37. StringvalidateCode, 38. intcodeLength) 39. BufferedImagebImg=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB); 40. Graphicsg=bImg.getGraphics(); 41. Randomrandom=newRandom(); 42. 43. if(null=validateCode|validateCode.isEmpty() 44. validateCode=Default_ValidateCode; 45. 46. if(fontSize=height) 47. fontSize=height-1; 48. 49. 50. drawOutline(g,width,height); 51. switch(disturbType) 52. caseDisturb_Type_Simple: 53. drawSimpleDisturb(g,random,width,height); 54. break; 55. caseDisturb_Type_Complex: 56. drawDisturb(g,random,width,height); 57. break; 58. default: 59. break; 60. 61. 62. Stringcode=drawCode(g,random,validateCode,codeLength,width, 63. height,fontSize); 64. g.dispose(); 65. try 66. ImageOutputStreamimOut=ImageIO.createImageOutputStream(bos); 67. ImageIO.write(bImg,JPEG,imOut); 68. imOut.close(); 69. catch(IOExceptione) 70. e.printStackTrace(); 71. 72. returncode; 73. 74. 75. /* 76. *绘制边框 77. * 78. *paramg 79. *paramwidth 80. *paramheight 81. */82. privatestaticvoiddrawOutline(Graphicsg,intwidth,intheight) 83. g.setColor(Color.white); 84. g.fillRect(0,0,width,height); 85. g.setColor(Color.BLACK); 86. g.drawRect(0,0,width-1,height-1); 87. 88. 89. /* 90. *绘制比较复杂的干扰线 91. * 92. *paramg 93. *paramrandom 94. *paramwidth 95. *paramheight 96. */97. privatestaticvoiddrawDisturb(Graphicsg,Randomrandom,intwidth, 98. intheight) 99. intx,y,x1,y1; 100. for(inti=0;iwidth;i+) 101. x=random.nextInt(width); 102. y=random.nextInt(height); 103. x1=random.nextInt(12); 104. y1=random.nextInt(12); 105. g.setColor(getRandomColor(random,120,255); 106. g.drawLine(x,y,x+x1,y+y1); 107. g.fillArc(x,y,x1,y1,random.nextInt(360),random.nextInt(360); 108. 109. 110. 111. /* 112. *绘制简单的干扰线 113. * 114. *paramg 115. *paramrandom 116. *paramwidth 117. *paramheight 118. */119. privatestaticvoiddrawSimpleDisturb(Graphicsg,Randomrandom,intwidth, 120. intheight) 121. g.setColor(getRandomColor(random,160,200); 122. for(inti=0;i255?255:Math.abs(pMax); 141. pMin=(Math

温馨提示

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

评论

0/150

提交评论