1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
| @RequestMapping(path = "/validation") public void validation(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setHeader("pragam", "No-cache"); response.setHeader("Cache", "No-cache"); response.setDateHeader("Expires", 0);
response.setContentType("image/gif");
int width = 100; int height = 50;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Font font = new Font("楷体", Font.BOLD, 30);
g.setFont(font); g.setColor(Color.orange); g.drawRect(5, 10, width, height); g.setColor(new Color(255, 247, 238)); g.fillRect(1, 1, width - 2, height - 2);
String content = "0123456789abcdefghijklmnopqrstuvwxyz"; String info = "";
for (int x = 0; x < 4; x++) {
int index = new Random().nextInt(content.length() - 1); char ch = content.charAt(index);
Random random = new Random();
g.setColor(new Color(255, 113, 0));
g.drawString(ch + "", 25 * x + 1, 20 + random.nextInt(30));
g.drawLine(random.nextInt(100), random.nextInt(50), random.nextInt(100), random.nextInt(50));
info += ch; }
HttpSession session = request.getSession(); session.setAttribute("validation", info);
g.dispose(); OutputStream out = response.getOutputStream(); ImageIO.write(image, "gif", out);
out.flush(); out.close(); }
|