Java生成圖片驗證碼_第1頁
Java生成圖片驗證碼_第2頁
Java生成圖片驗證碼_第3頁
Java生成圖片驗證碼_第4頁
Java生成圖片驗證碼_第5頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、 /* * * version */ public class Image extends HttpServlet /* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * param request servlet request * param response servlet response */ protected void processRequest(HttpServletRequest request, HttpSer

2、vletResponse response) throws ServletException, IOException response.setContentType("image/jpeg"); response.setHeader("Pragma","No-cache"); response.setHeader("Cache-Control","no-cache"); response.setDateHeader("Expires", 0); HttpSession se

3、ssion=request.getSession(); / ÔÚÄÚ´æÖд´½¨Í¼Ïó int width=60, height=20; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); / »ñȡͼÐÎÉÏ&#

4、207;ÂÎÄ Graphics g = image.getGraphics(); /Éú³ÉËæ»úÀà Random random = new Random(); / É趨±³¾°É« g.setColor(getRandColor(200,250); g.fillRect(0, 0, width, height); /Éè¶&#

5、168;×ÖÌå g.setFont(new Font("Times New Roman",Font.PLAIN,18); /»­±ß¿ò /g.setColor(new Color(); /g.drawRect(0,0,width-1,height-1); / Ëæ»ú²úÉú155Ìõ¸ÉÈÅÏߣ

6、72;ʹͼÏóÖеÄÈÏÖ¤Âë²»Ò×±»ÆäËü³ÌÐò̽²âµ½ g.setColor(getRandColor(160,200); for (int i=0;i<155;i+) int x = random.nextI

7、nt(width); int y = random.nextInt(height); int xl = random.nextInt(12); int yl = random.nextInt(12); g.drawLine(x,y,x+xl,y+yl); / È¡Ëæ»ú²úÉúµÄÈÏÖ¤Âë(4λÊý×Ö) String sRand="&qu

8、ot; for (int i=0;i<4;i+) String rand=String.valueOf(random.nextInt(10); sRand+=rand; / ½«ÈÏÖ¤ÂëÏÔʾµ½Í¼ÏóÖÐ g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)

9、;/µ÷Óú¯Êý³öÀ´µÄÑÕÉ«Ïàͬ£¬¿ÉÄÜÊÇÒòΪÖÖ×ÓÌ«½Ó½ü£¬ËùÒ

10、2;Ö»ÄÜÖ±½ÓÉú³É g.drawString(rand,13*i+6,16); / ½«ÈÏÖ¤Âë´æÈëSESSION session.setAttribute("rand",sRand); / ͼÏóÉúЧ g.dispose(); Ser

11、vletOutputStream responseOutputStream =response.getOutputStream(); / Êä³öͼÏóµ½Ò³Ãæ ImageIO.write(image, "JPEG", responseOutputStream); /ÒÔϹرÕÊäÈëÁ÷

12、;£¡ responseOutputStream.flush(); responseOutputStream.close(); Color getRandColor(int fc,int bc)/¸ø¶¨·¶Î§»ñµÃËæ»úÑÕÉ« Random random = new Random(); if(fc>255) fc=255; if(bc>255) bc=25

13、5; int r=fc+random.nextInt(bc-fc); int g=fc+random.nextInt(bc-fc); int b=fc+random.nextInt(bc-fc); return 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>

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

15、od. * param request servlet request * param response servlet response */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException processRequest(request, response); /* Returns a short description of the servlet. */ public String getServletI

16、nfo() return "Short description" / </editor-fold> -ʹÓÃÑéÖ¤ÂëͼƬµÄÎļþ-index.jsp- <% page contentType="text/html;charset=gb2312" %> <!DOCTYPE HTML PUBLIC "-/W

17、3C/DTD HTML 4.01 Transitional/EN"> <html> <head> <title>ÈÏÖ¤ÂëÊäÈëÒ³Ãæ</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312"> <META HTTP-EQUIV=&quo

18、t;Pragma" CONTENT="no-cache"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-cache"> <META HTTP-EQUIV="Expires" CONTENT="0"> </head> <body> <form method=post action="check.jsp"> <table> <tr> &

19、lt;td align=left>ϵͳ²úÉúµÄÈÏÖ¤Â룺</td> </tr> <tr> <td align=left>ÊäÈëÉÏÃæµÄÈÏÖ¤Â룺</td&

20、gt; <td><input type=text name=rand maxlength=4 value=""></td> </tr> <tr> <td colspan=2 align=center><input type=submit value="Ìá½»¼ì²â"></td> </tr> </form> </body> </html&

21、gt; -ÑéÖ¤µÄÒ³Ãæ-check.jsp- <html> <head> <title>ÈÏÖ¤ÂëÑéÖ¤Ò³Ãæ</title> <meta http-equiv="Content-Type" content="text/html; charset=gb2312&

22、quot;> <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("rand"); String input = request.getPar

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論