• Java生成算式验证码


    1. @WebServlet("/captcha")
    2. public class CaptchaServlet extends HttpServlet {
    3. private static final long serialVersionUID = 1L;
    4. private static final int WIDTH = 100; // 图片宽度
    5. private static final int HEIGHT = 30; // 图片高度
    6. private static final int FONT_SIZE = 20; // 字体大小
    7. private static final int MAX_NUMBER = 10; // 随机数的最大值
    8. private static final int MAX_OFFSET = 5; // 随机误差的最大值
    9. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    10. // 生成随机算式
    11. Random random = new Random();
    12. int num1 = random.nextInt(MAX_NUMBER);
    13. int num2 = random.nextInt(MAX_NUMBER);
    14. char op = random.nextBoolean() ? '+' : '-';
    15. int result = op == '+' ? num1 + num2 : num1 - num2;
    16. String captcha = String.format("%d %c %d = ", num1, op, num2);
    17. // 将验证码和计算结果存储到 session 中
    18. HttpSession session = request.getSession();
    19. session.setAttribute("captcha", captcha);
    20. session.setAttribute("result", result);
    21. // 生成验证码图片
    22. BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
    23. Graphics2D g = image.createGraphics();
    24. g.setColor(Color.WHITE);
    25. g.fillRect(0, 0, WIDTH, HEIGHT);
    26. g.setColor(Color.BLACK);
    27. g.setFont(new Font("Arial", Font.PLAIN, FONT_SIZE));
    28. g.drawString(captcha, MAX_OFFSET, HEIGHT / 2 + FONT_SIZE / 2 - MAX_OFFSET);
    29. // 将验证码图片写入输出流中
    30. response.setContentType("image/png");
    31. OutputStream out = response.getOutputStream();
    32. ImageIO.write(image, "png", out);
    33. out.close();
    34. }
    35. }
    36. Result res = new Result();
    37. // 生成随机算数运算符
    38. final Random random = new Random();
    39. int operatorIndex = random.nextInt(3);
    40. char operator = '+';
    41. switch (operatorIndex) {
    42. case 0:
    43. operator = '+';
    44. break;
    45. case 1:
    46. operator = '-';
    47. break;
    48. case 2:
    49. operator = '×';
    50. break;
    51. }
    52. // 生成随机算数操作数
    53. int operand1 = random.nextInt(10) + 1;
    54. int operand2 = random.nextInt(10) + 1;
    55. if (operator == '-') {
    56. while (operand1 < operand2) {
    57. operand1 = random.nextInt(10) + 1;
    58. operand2 = random.nextInt(10) + 1;
    59. }
    60. }
    61. // 计算结果
    62. int result = 0;
    63. String lowerCaseCode = "";
    64. switch (operator) {
    65. case '+':
    66. result = operand1 + operand2;
    67. lowerCaseCode = operand1 + "+" + operand2 + "=";
    68. break;
    69. case '-':
    70. result = operand1 - operand2;
    71. lowerCaseCode = operand1 + "-" + operand2 + "=";
    72. break;
    73. case '×':
    74. result = operand1 * operand2;
    75. lowerCaseCode = operand1 + "*" + operand2 + "=";
    76. break;
    77. }

    第一部分是一个名为 CaptchaServlet 的 Java Servlet,用于生成验证码图片。第二部分是一个生成算式验证码的方法,返回一个包含算式和结果的字符串,以及一个 Result 对象。

    在 CaptchaServlet 中,首先生成一个随机算式,包含两个随机数和一个随机运算符,然后将验证码和计算结果存储到 HttpSession 中。接着生成一个验证码图片,使用 Graphics2D 绘制验证码字符串,最后将验证码图片写入 HttpServletResponse 的输出流中。

    在生成算式验证码的方法中,首先生成一个随机运算符和两个随机操作数,如果运算符是减法,则要保证被减数大于减数。然后根据运算符计算结果,并将算式和结果存储到一个字符串中,返回给调用者。同时,还生成一个 Result 对象,用于存储算式验证码的字符串和计算结果。

  • 相关阅读:
    转义字符串
    QA26\28
    ubuntu20 安装 cmake 3.27
    像用Excel一样用Python:pandasGUI
    Java学习----前端2
    c语言内功修炼--深度剖析数据的存储
    springboot+vue前后端音乐网系统,挺漂亮的
    vue的路由与nodejs的环境搭建
    javascipt中对象和数组的遍历,for in 和for of的区别是什么
    GIS、遥感和生态等领域常用数据集汇总【数据集】
  • 原文地址:https://blog.csdn.net/m0_49790240/article/details/133787134