SpringMVC生成验证码图片不显示怎么办

  介绍

这篇文章主要介绍SpringMVC生成验证码图片不显示怎么办,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

近期用导弹框架写一个项目,登录模块需要生成验证码图片,我把相关的代码写好了之后传到jsp,但是图片不显示,查看控制台显示404年反复查询了一下代码并没有发现任何问题,代码如下:

@ controller   public  class  ValidateCodeController  {   private 才能static  final  long  serialVersionUID =, 1 l;   private 才能static  int  WIDTH =, 70,,,,,,,,,//,验证码图片的宽度   private 才能static  int  HEIGHT =, 35岁,,,,,,,//,验证码图片的高度   @RequestMapping才能(“/validateCode")   public 才能;void  validateCode (HttpServletRequest 请求,HttpServletResponse 响应),throws  ServletException, IOException  {   ,,,HttpSession  session =, request.getSession ();   ,,,response.setContentType(“图像/jpeg");   ,,,ServletOutputStream  sos =, response.getOutputStream ();   ,,,//,设置浏览器不要缓存该图片   ,,,response.setHeader (“Pragma",,“No-cache");   ,,,response.setHeader (“Cache-Control",,“no-cache");   ,,,response.setDateHeader (“Expires",, 0);   ,,,//,创建图片缓存区   ,,,BufferedImage  image =, new  BufferedImage(宽度、高度,祝福;BufferedImage.TYPE_INT_RGB);   ,,,Graphics  g =, image.getGraphics(),,,,,,,,,,,//,获取画笔   ,,,char  [], rands =, generateCheckCode(),,,,,,,//,产生随机的验证码   ,,,//,产生图像   ,,,drawBackground (g);   ,,,drawRands (g,兰德);   ,,,//,结束绘画过程,完成图像   ,,,g.dispose ();   ,,,//,将图像输出到客户端   ,,,ByteArrayOutputStream  bos =, new  ByteArrayOutputStream ();   ,,,ImageIO.write(图像,,“JPEG",, bos);   ,,,byte  [], buf =, bos.toByteArray ();   ,,,response.setContentLength (buf.length);   ,,,sos.write (buf);   ,,,bos.close ();   ,,,sos.close ();   ,,,//,将当前验证码存入,Session 中   ,,,session.setAttribute (“valicode",, new 字符串(兰德));   ,,,//System.out.println (“1:“+ request.getSession () .getId ());   ,,,System.out.println (session.getAttribute (“valicode"));   ,,}   private 才能;void  drawRands (g, Graphics  char[],兰特),{   ,,,g.setColor (new 颜色(41岁,41岁,41));   ,,,g.setFont (new 字体(null,, Font.ITALIC | Font.BOLD,, 18));   ,,,g.drawString(““, +,兰德[0],,2,,17);   ,,,g.drawString(“,“, +,兰德[1],,16日,20);   ,,,g.drawString(“,“, +,兰德[2],,31日,13);   ,,,g.drawString(“,“, +,兰德[3],,46岁,18);   ,,,//System.out.println(兰德);   ,,}   private 才能;void  drawBackground (Graphics  g), {   ,,,g.setColor (new 颜色(255,,255,,255));   ,,,g.fillRect(0, 0,宽度,高度);   ,,,//,随机产生,120,个干扰点   ,,,(int 小姐:=,0;,小姐:& lt;, 120;,我+ +),{   ,,,,,int  x =, (int), (math . random(), *,宽度);   ,,,,,int  y =, (int), (math . random(), *,高度);   ,,,,,int  red =, (int), (math . random (), *, 255);   ,,,,,int  green =, (int), (math . random (), *, 255);   ,,,,,int  blue =, (int), (math . random (), *, 255);   ,,,,,g.setColor (new 颜色(红色,绿色,蓝色));   ,,,,,g.drawOval (x, y,, 1,, 0);   ,,,}   ,,}//,才能生成四个字符的验证码   private 才能;char [], generateCheckCode (), {   ,,,String  chars =,“0123456789 abcdefghijklmnopqrstuvwxyz";   ,,,char  [], rands =, new  char [4];   null   null   null   null   null   null   null

SpringMVC生成验证码图片不显示怎么办