code.jsp
上传用户:manager48
上传日期:2022-07-31
资源大小:997k
文件大小:3k
源码类别:

Jsp/Servlet

开发平台:

Java

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