我思故我強

          JSP驗證碼生成

          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 李云澤 閱讀(207) 評論(0)  編輯  收藏 所屬分類: Java代碼

          主站蜘蛛池模板: 增城市| 汝南县| 台东县| 疏附县| 白银市| 布拖县| 十堰市| 贵阳市| 扬中市| 荥经县| 平陆县| 闻喜县| 崇礼县| 海伦市| 建湖县| 鄢陵县| 佛山市| 海盐县| 铁岭县| 北宁市| 翼城县| 湾仔区| 南充市| 从化市| 阜阳市| 礼泉县| 监利县| 彩票| 栾川县| 克东县| 荃湾区| 平和县| 邳州市| 象州县| 东乡| 庆安县| 临朐县| 贺兰县| 文登市| 泸水县| 旬阳县|