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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
| package loginCode;
import javax.imageio.ImageIO; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.*; import java.awt.image.BufferedImage; import java.io.IOException; import java.util.HashMap; import java.util.Random;
@WebServlet("/generateCode") public class GenerateCode extends HttpServlet { private static final int WIDTH = 120; private static final int HEIGHT = 30;
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { BufferedImage bufferedImage = new BufferedImage(WIDTH, HEIGHT, 1); Graphics graphics = bufferedImage.getGraphics(); setBackground(graphics); setBorder(graphics); setRandomLine(graphics); String randomString = getRandomString((Graphics2D) graphics, "mixed"); System.out.println("本次生成的验证码是: " + randomString); req.getSession().setAttribute("checkCode", randomString); resp.setContentType("image/jpg"); ImageIO.write(bufferedImage, "jpg", resp.getOutputStream()); }
private String getRandomString(Graphics2D graphics, String type) { graphics.setColor(Color.RED); graphics.setFont(new Font("宋体", Font.BOLD, 20)); HashMap<String, String> map = new HashMap<>(3); map.put("number", "0123456789"); map.put("letter", "QWERTYUIOPASDFGHJKLZXCVBNM"); map.put("mixed", "1234567890QWERTYUIOPASDFGHJKLZXCVBNM");
String randomString = setRandomString(graphics, map.get(type)); return randomString; }
private String setRandomString(Graphics2D graphics, String s) { StringBuilder sb = new StringBuilder(); int x = 5; String chars = ""; int num = 4; for (int i = 0; i < num; i++) { int degree = new Random().nextInt() % 30; chars = s.charAt(new Random().nextInt(s.length() - 1)) + ""; sb.append(chars);
graphics.rotate(degree * Math.PI / 180, x, 20); graphics.drawString(chars, x, 20); x += 30; } return sb.toString(); }
private void setRandomLine(Graphics graphics) { graphics.setColor(Color.GREEN);
int num = 5; for (int i = 0; i < num; i++) { int x1 = new Random().nextInt(WIDTH); int y1 = new Random().nextInt(HEIGHT);
int x2 = new Random().nextInt(WIDTH); int y2 = new Random().nextInt(HEIGHT);
graphics.drawLine(x1, y1, x2, y2); }
}
private void setBackground(Graphics graphics) { graphics.setColor(Color.white); graphics.fillRect(0, 0, WIDTH, HEIGHT); }
private void setBorder(Graphics graphics) { graphics.setColor(Color.BLUE); graphics.drawRect(1, 1, WIDTH - 2, HEIGHT - 2); } }
|