我思故我強(qiáng)

          JSP驗(yàn)證碼生成

          java 代碼
          1. <%@?page ??
          2. ???????? import = "java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %> ??
          3. <%@?page? import = "java.io.OutputStream" %> ??
          4. <%@?page?pageEncoding= "UTF-8" %> ??
          5. <%!Color?getRandColor( int ?fc,? int ?bc)?{ ??
          6. ????????????????Random?random?=? new ?Random(); ??
          7. ???????????????? if ?(fc?>? 255 ) ??
          8. ????????????????????????fc?=? 255 ; ??
          9. ???????????????? if ?(bc?>? 255 ) ??
          10. ????????????????????????bc?=? 255 ; ??
          11. ???????????????? int ?r?=?fc?+?random.nextInt(bc?-?fc); ??
          12. ???????????????? int ?g?=?fc?+?random.nextInt(bc?-?fc); ??
          13. ???????????????? int ?b?=?fc?+?random.nextInt(bc?-?fc); ??
          14. ???????????????? return ? new ?Color(r,?g,?b); ??
          15. ????????}%> ??
          16. <% ??
          17. ???????????????? try ?{ ??
          18. ????????????????response.setHeader( "Pragma" ,? "No-cache" ); ??
          19. ????????????????response.setHeader( "Cache-Control" ,? "no-cache" ); ??
          20. ????????????????response.setDateHeader( "Expires" ,? 0 ); ??
          21. ???????????????? int ?width?=? 60 ,?height?=? 20 ; ??
          22. ????????????????BufferedImage?image?=? new ?BufferedImage(width,?height, ??
          23. ????????????????BufferedImage.TYPE_INT_RGB); ??
          24. ????????????????OutputStream?os?=?response.getOutputStream(); ??
          25. ????????????????Graphics?g?=?image.getGraphics(); ??
          26. ????????????????Random?random?=? new ?Random(); ??
          27. ????????????????g.setColor(getRandColor( 200 ,? 250 )); ??
          28. ????????????????g.fillRect( 0 ,? 0 ,?width,?height); ??
          29. ??
          30. ????????????????g.setFont( new ?Font( "Times?New?Roman" ,?Font.PLAIN,? 18 )); ??
          31. ????????????????g.setColor(getRandColor( 160 ,? 200 )); ??
          32. ???????????????? for ?( int ?i?=? 0 ;?i?<? 155 ;?i++)?{ ??
          33. ???????????????????????? int ?x?=?random.nextInt(width); ??
          34. ???????????????????????? int ?y?=?random.nextInt(height); ??
          35. ???????????????????????? int ?xl?=?random.nextInt( 12 ); ??
          36. ???????????????????????? int ?yl?=?random.nextInt( 12 ); ??
          37. ????????????????????????g.drawLine(x,?y,?x?+?xl,?y?+?yl); ??
          38. ????????????????} ??
          39. ????????????????String?sRand?=? "" ; ??
          40. ???????????????? for ?( int ?i?=? 0 ;?i?<? 4 ;?i++)?{ ??
          41. ????????????????????????String?rand?=?String.valueOf(random.nextInt( 10 )); ??
          42. ????????????????????????sRand?+=?rand; ??
          43. ????????????????????????g.setColor( new ?Color( 20 ?+?random.nextInt( 110 ),? 20 ?+?random ??
          44. ????????????????????????.nextInt( 110 ),? 20 ?+?random.nextInt( 110 ))); ??
          45. ????????????????????????g.drawString(rand,? 13 ?*?i?+? 6 ,? 16 ); ??
          46. ????????????????} ??
          47. ????????????????session.setAttribute( "rand" ,?sRand); ??
          48. ????????????????g.dispose(); ??
          49. ??
          50. ????????????????ImageIO.write(image,? "JPEG" ,?os); ??
          51. ????????????????os.flush(); ??
          52. ????????????????os.close(); ??
          53. ????????????????os?=? null ; ??
          54. ????????????????response.flushBuffer(); ??
          55. ????????????????out.clear(); ??
          56. ????????????????out?=?pageContext.pushBody(); ??
          57. ????????}? catch ?(IllegalStateException?e)?{ ??
          58. ????????????????System.out.println(e.getMessage()); ??
          59. ????????????????e.printStackTrace(); ??
          60. ????????} ??
          61. %> ??

          posted on 2007-09-20 19:32 李云澤 閱讀(203) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): Java代碼

          主站蜘蛛池模板: 安多县| 宁强县| 利辛县| 常宁市| 齐河县| 平南县| 广东省| 大冶市| 濮阳市| 五家渠市| 绵阳市| 金昌市| 汽车| 辽源市| 浦江县| 措美县| 从江县| 武冈市| 阳山县| 金川县| 安西县| 林西县| 航空| 宝鸡市| 鸡西市| 太保市| 银川市| 营山县| 鹤岗市| 施秉县| 广平县| 永宁县| 沽源县| 合川市| 垣曲县| 商河县| 社旗县| 聂荣县| 德钦县| 那曲县| 楚雄市|