我思故我強

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

          主站蜘蛛池模板: 望奎县| 鹿邑县| 获嘉县| 镇坪县| 安庆市| 平南县| 侯马市| 芦溪县| 小金县| 兴隆县| 垦利县| 栖霞市| 勃利县| 建阳市| 张家港市| 安化县| 郁南县| 霍山县| 固安县| 海城市| 裕民县| 常宁市| 柳河县| 万全县| 马尔康县| 安乡县| 沽源县| 大余县| 澄江县| 调兵山市| 苗栗市| 昌邑市| 湛江市| 东城区| 兰考县| 栾川县| 封开县| 延津县| 茶陵县| 重庆市| 合作市|