在 web.xml里面加入 下面的代码:
<servlet>
<servlet-name>validatecode</servlet-name>
<servlet-class>webapp.servlet.ValidateCodeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>validatecode</servlet-name>
<url-pattern>/images/validatecode.jpeg</url-pattern>
</servlet-mapping>
servlet类:
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import webapp.auth.SessionUtil; //项目使用自己编写的session处理类
/**
* 验证码生成servlet
* @author Administrator
*
*/
public class ValidateCodeServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
int w = 60;
int h = 20;
int fh = 10;
int cy = 0;
int x = 1;
int cc = 4;
char[] cs = { 'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q'
,'R','S','T','U','V','W','X','Y','Z', '2', '3', '4', '5', '6',
'7', '8', '9'
};
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, java.io.IOException {
x = w / (cc + 1);
fh = h - 2;
cy = h - 4;
// image buffer
BufferedImage buffImg = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = buffImg.createGraphics();
Random random = new Random();
// fill image with white color
g.setColor(Color.WHITE);
g.fillRect(0, 0, w, h);
// create font
Font font = new Font("Fixedsys", Font.PLAIN | Font.BOLD, fh);
// set font
g.setFont(font);
g.setColor(Color.blue);
g.drawRect(0, 0, w - 1, h - 1);
// draw 200 radom line
g.setColor(Color.gray);
for (int i = 0; i < 60; i++) {
int x1 = random.nextInt(w);
int y1 = random.nextInt(h);
int x2 = random.nextInt(12);
int y2 = random.nextInt(12);
g.drawLine(x1, y1, x1 + x2, y1 + y2);
}
// draw radom code
StringBuffer randomCode = new StringBuffer();
int red = 0, green = 0, blue = 0;
for (int i = 0; i < cc; i++) {
String strRand = String.valueOf(cs[random.nextInt(cs.length - 1)]);
red = random.nextInt(120);
green = random.nextInt(60);
blue = random.nextInt(200);
g.setColor(new Color(red, green, blue));
g.drawString(strRand, (i + 1) * x - 6, cy);
randomCode.append(strRand);
}
HttpSession session = req.getSession();
SessionUtil.set(req, SessionUtil.VALIDATE_CODE_KEY, randomCode
.toString());
resp.setHeader("Pragma", "no-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
resp.setContentType("image/jpeg");
ServletOutputStream sos = resp.getOutputStream();
ImageIO.write(buffImg, "jpeg", sos);
sos.close();
}
}
javax.imageio.IIOException: Can't create output stream! javax.imageio.ImageIO.write(ImageIO.java:1521)
网上搜此问题,有说法是与JDK有关,并给出解决方案:
把
ImageIO.write(image, "jpeg", response.getOutputStream());
修改为
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(response.getOutputStream()); encoder.encode(image);
//jsp上要增加com.sun.image.codec.jpeg.JPEGCodec,com.sun.image.codec.jpeg.JPEGImageEncoder这些类的import
经验证,是可以解决问题的。
但是。。。,原因怎会是JDK?我这个环境几分钟前还是正常的,中间没有安装过JDK或者修改环境变量
检查tomcat的日志,终于真相大白:
javax.imageio.IIOException: Can't create output stream! at javax.imageio.ImageIO.write(ImageIO.java:1521) Caused by: javax.imageio.IIOException: Can't create cache file! at javax.imageio.ImageIO.createImageOutputStream(ImageIO.java:395) at javax.imageio.ImageIO.write(ImageIO.java:1519) ... 34 more Caused by: java.io.IOException: 系统找不到指定的路径。
原来是ImageIO.write(image, "jpeg", response.getOutputStream());
需要写缓冲文件,但是写文件不成功。
回忆几分钟前的操作,可能是删除了tomcat的temp目录。
手动创建temp目录,问题解决!