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(200250));   
  28.                 g.fillRect(00, width, height);   
  29.   
  30.                 g.setFont(new Font("Times New Roman", Font.PLAIN, 18));   
  31.                 g.setColor(getRandColor(160200));   
  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 + 616);   
  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. %>   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值