我思故我强

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 李云泽 阅读(188) 评论(0)  编辑  收藏 所属分类: Java代码


只有注册用户登录后才能发表评论。


网站导航: