vcode.jsp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <%@ page contentType="image/jpeg" import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" pageEncoding="UTF-8"%>
  2. <%!Color getRandColor(int fc, int bc) {
  3. Random random = new Random();
  4. if (fc > 255)
  5. fc = 255;
  6. if (bc > 255)
  7. bc = 255;
  8. int r = fc + random.nextInt(bc - fc);
  9. int g = fc + random.nextInt(bc - fc);
  10. int b = fc + random.nextInt(bc - fc);
  11. return new Color(r, g, b);
  12. }
  13. char[] codeSequence = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
  14. 'X', 'Y', '3', '4', '5', '6', '7', '8', '9' };
  15. %>
  16. <%
  17. response.setHeader("Pragma", "No-cache");
  18. response.setHeader("Cache-Control", "no-cache");
  19. response.setDateHeader("Expires", 0);
  20. int width = 80, height = 20;
  21. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  22. Graphics g = image.getGraphics();
  23. Random random = new Random();
  24. g.setColor(getRandColor(200, 250));
  25. g.fillRect(0, 0, width, height);
  26. g.setFont(new Font("Times New Roman", Font.BOLD, 18));
  27. g.setColor(new Color(0, 0, 0));
  28. g.drawRect(0, 0, width - 1, height - 1);
  29. g.setColor(getRandColor(160, 200));
  30. for (int i = 0; i < 155; i++) {
  31. int x = random.nextInt(width);
  32. int y = random.nextInt(height);
  33. int xl = random.nextInt(12);
  34. int yl = random.nextInt(12);
  35. g.drawLine(x, y, x + xl, y + yl);
  36. }
  37. String sRand = "";
  38. for (int i = 0; i < 4; i++) {
  39. String rand = String.valueOf(codeSequence[random.nextInt(30)]);
  40. sRand += rand;
  41. g.setColor(new Color(random.nextInt(80), random.nextInt(80), random.nextInt(80)));
  42. g.drawString(rand, 15 * i + 10, 16);
  43. }
  44. session.setAttribute("validcode", sRand);
  45. g.dispose();
  46. ServletOutputStream sos = response.getOutputStream();
  47. ImageIO.write(image, "jpeg", sos);
  48. sos.flush();
  49. sos.close();
  50. sos = null;
  51. response.flushBuffer();
  52. out.clear();
  53. out = pageContext.pushBody();
  54. %>