Java生成图片验证码_第1页
Java生成图片验证码_第2页
Java生成图片验证码_第3页
Java生成图片验证码_第4页
Java生成图片验证码_第5页
全文预览已结束

下载本文档

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

文档简介

1、 import java.io.*; import .*; import javax.servlet.*; import javax.servlet.http.*; import java.awt.*; import java.awt.image.*; import java.util.*; import javax.imageio.*; /* * * author * version */ public class Image extends HttpServlet /* Processes requests for both HTTP <code

2、>GET</code> and <code>POST</code> methods. * param request servlet request * param response servlet response */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException response.setContentType("image/jpeg&q

3、uot;); response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); HttpSession session=request.getSession(); / ÔÚÄÚ´æÖд´½¨

4、ͼÏó int width=60, height=20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); / »ñȡͼÐÎÉÏÏÂÎÄ Graphics g = image.getGraphics(); /Éú³ÉËæ»&#

5、250;Àà Random random = new Random(); / É趨±³¾°É« g.setColor(getRandColor(200,250); g.fillRect(0, 0, width, height); /É趨×ÖÌå g.setFont(new Font("Times New Roman",Font.PLAIN,18); /»

6、73;±ß¿ò /g.setColor(new Color(); /g.drawRect(0,0,width-1,height-1); / Ëæ»ú²úÉú155Ìõ¸ÉÈÅÏߣ¬Ê¹Í¼ÏóÖеÄÈÏÖ¤Âë

7、78;»Ò×±»ÆäËü³ÌÐò̽²âµ½ g.setColor(getRandColor(160,200); for (int i=0;i<155;i+) int x = random.nextInt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g

8、.drawLine(x,y,x+xl,y+yl); / È¡Ëæ»ú²úÉúµÄÈÏÖ¤Âë(4λÊý×Ö) String sRand="" for (int i=0;i<4;i+) String rand=String.valueOf(random.nextInt(10); sRand+=rand; / ½«

9、ÈÏÖ¤ÂëÏÔʾµ½Í¼ÏóÖÐ g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110);/µ÷Óú¯Êý³öÀ´µÄÑÕ

10、1;«Ïàͬ£¬¿ÉÄÜÊÇÒòΪÖÖ×ÓÌ«½Ó½ü£¬ËùÒÔÖ»ÄÜÖ±½ÓÉú³É g.drawString(rand,13*i+6,16

11、); / ½«ÈÏÖ¤Âë´æÈëSESSION session.setAttribute("rand",sRand); / ͼÏóÉúЧ g.dispose(); ServletOutputStream responseOutputStream =response.getOutputStream(); / Êä³öÍ

12、88;Ïóµ½Ò³Ãæ ImageIO.write(image, "JPEG", responseOutputStream); /ÒÔϹرÕÊäÈëÁ÷£¡ responseOutputStream.flush(); responseOutputStream.close(); Color getRandColor(int fc,in

13、t bc)/¸ø¶¨·¶Î§»ñµÃËæ»úÑÕÉ« Random random = new Random(); if(fc>255) fc=255; if(bc>255) bc=255; int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); ret

14、urn new Color(r,g,b); / <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."> /* Handles the HTTP <code>GET</code> method. * param request servlet request * param response servlet response */ protect

15、ed void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException processRequest(request, response); /* Handles the HTTP <code>POST</code> method. * param request servlet request * param response servlet response */ protected void doPost(HttpSer

16、vletRequest request, HttpServletResponse response) throws ServletException, IOException processRequest(request, response); /* Returns a short description of the servlet. */ public String getServletInfo() return "Short description" / </editor-fold> -ʹÓÃÑé

17、;Ö¤ÂëͼƬµÄÎļþ-index.jsp- <% page contentType="text/html;charset=gb2312" %> <!DOCTYPE HTML PUBLIC "-/W 3C/DTD HTML 4.01 Transitional/EN"> <html> <head> <title>ÈÏÖ&#

18、164;ÂëÊäÈëÒ³Ãæ</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Cache-Control" CONTENT=

19、"no-cache"> <META HTTP-EQUIV="Expires" CONTENT="0"> </head> <body> <form method=post action="check.jsp"> <table> <tr> <td align=left>ϵͳ²úÉúµÄÈÏÖ

20、4;Â룺</td> <td><img border=0 src=" </tr> <tr> <td align=left>ÊäÈëÉÏÃæµÄÈÏÖ¤Â룺</td> <td><input type=text name=rand maxlength=4 value=&q

21、uot;"></td> </tr> <tr> <td colspan=2 align=center><input type=submit value="Ìá½»¼ì²â"></td> </tr> </form> </body> </html> -ÑéÖ¤µÄÒ³Ãæ

22、;-check.jsp- <% page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %> <html> <head> <title>ÈÏÖ¤ÂëÑéÖ¤Ò³Ãæ</title> <

23、;meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <META HTTP-EQUIV="Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="0"> </head> <body> <% String rand = (String)session.getAttribute("ran

温馨提示

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

评论

0/150

提交评论