基本上和前面的那个差不多,但是稍微复杂一点

    CaptcahCode.java

    1. package com.imooc.code;
    2. import javax.imageio.ImageIO;
    3. import javax.servlet.http.HttpServletRequest;
    4. import javax.servlet.http.HttpServletResponse;
    5. import java.awt.*;
    6. import java.awt.font.FontRenderContext;
    7. import java.awt.geom.Rectangle2D;
    8. import java.awt.image.BufferedImage;
    9. import java.io.IOException;
    10. import java.util.Random;
    11. public class CaptcahCode {
    12. /**
    13. * 算术表达式验证码
    14. * 1、干扰线的产生
    15. * 2、范围随机颜色,随机数
    16. * @param response
    17. * @return
    18. */
    19. public static String drawImageVericate(HttpServletResponse response){
    20. //定义验证码的宽度和高度
    21. int width = 100,height = 30;
    22. //在内存中创建图片
    23. BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
    24. //创建图片的上下文
    25. Graphics2D g = image.createGraphics();
    26. //产生随机对象,此随机对象主要用户算术表达式的数字
    27. Random random = new Random();
    28. //设置背景
    29. g.setColor(getRandomColor(240,250));
    30. //设置字体
    31. g.setFont(new Font("微软雅黑",Font.PLAIN,22));
    32. //开始绘制
    33. g.fillRect(0,0,width,height);
    34. //干扰线的绘制,绘制线条到图片中
    35. g.setColor(getRandomColor(180,230));
    36. for (int i = 0; i < 100; i++) {
    37. int x = random.nextInt(width);
    38. int y = random.nextInt(height);
    39. int x1 = random.nextInt(60);
    40. int y1 = random.nextInt(60);
    41. //使用drawLine画线
    42. g.drawLine(x,y,x1,y1);
    43. }
    44. //开始进行对算输验证码表达式的拼接
    45. //这里注意,math的括号要包住后面的*10,不然会先计算(int)Math.random(),因为这个值介于0,1,一直都会是0
    46. int num1 = (int)(Math.random()*10) +1;
    47. int num2 = (int)(Math.random()*10) +1;
    48. System.out.println(num1);
    49. System.out.println(num2);
    50. //符号,产生一个[0,2]之间的随机数,0,1,2用于表示加减乘
    51. int symbol = random.nextInt(3);
    52. String symbolstr = "";
    53. int result =0;
    54. switch (symbol){
    55. case 0:symbolstr = "+";result = num1 + num2;break;
    56. case 1:symbolstr = "-";result = num1 - num2;break;
    57. case 2:symbolstr = "*";result = num1 * num2;break;
    58. }
    59. //拼接计算表达式,用于图片显示
    60. String calc = num1 + " " + symbolstr +" "+num2 + "=" +"?";
    61. //设置随机颜色
    62. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
    63. //绘制表达式
    64. g.drawString(calc,5,23);
    65. //结束绘制
    66. g.dispose();
    67. try {
    68. //输出图片到页面
    69. ImageIO.write(image,"jpg",response.getOutputStream());
    70. return String.valueOf(result);
    71. }catch (Exception ex){
    72. ex.printStackTrace();
    73. return null;
    74. }
    75. }
    76. /**
    77. * 获取随机颜色
    78. * @param fc
    79. * @param bc
    80. * @return
    81. */
    82. public static Color getRandomColor(int fc,int bc){
    83. Random random = new Random();
    84. //随机颜色,颜色的组成有三个参数,red,green和blue
    85. if(fc>255)fc = 255;
    86. if(bc>255)bc = 255;
    87. int r = fc + random.nextInt(bc-fc);
    88. int g = fc + random.nextInt(bc-fc);
    89. int b = fc + random.nextInt(bc-fc);
    90. Color random_color = new Color(r,g,b);
    91. return random_color;
    92. }
    93. }

    code.jsp

    1. <%@ page import="com.imooc.code.CaptcahCode" %>
    2. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    3. <%
    4. //1、清空浏览器缓存,保证验证码图片来自服务器而不是本地缓存
    5. response.setHeader("pragma","no-cache");
    6. response.setHeader("cache-control","no-cache");
    7. response.setHeader("expires","0");
    8. //2、调用编写的生成验证码的工具
    9. String code = CaptcahCode.drawImageVericate(response);
    10. session.setAttribute("code",code);
    11. //我这里遇到一个问题,在firefox和ie没问题,用chrome就打不开,而且还报错ERR_INCOMPLETE_CHUNKED_ENCODING,然后执行下面的第三步操作,chrome就可以了...,
    12. //3、解决getOutputStream异常问题,但是不知道为啥这么些
    13. out.clear();
    14. out = pageContext.pushBody();
    15. %>

    index.jsp

    1. <%--
    2. Created by IntelliJ IDEA.
    3. User: msi
    4. Date: 2020/7/4
    5. Time: 8:47
    6. To change this template use File | Settings | File Templates.
    7. --%>
    8. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    9. <html>
    10. <head>
    11. <title>验证码</title>
    12. </head>
    13. <body>
    14. <img src="code.jsp" alt="oops" id = "code">
    15. <a href="javascript:void(0);" onclick="changeCode()">换一张</a>
    16. <script>
    17. function changeCode() {
    18. //这里加?time=是为了避免浏览器缓存而导致加载不出验证码
    19. document.getElementById("code").src = "code.jsp"+"?time="+new Date().getTime();
    20. }
    21. </script>
    22. </body>
    23. </html>