日期:2014-05-20 浏览次数:20670 次
// 产生随机认证码
char[] rands = generateCheckCode();
// 创建内存图象并获得其图形上下文
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
// 产生图像
drawBackground(g);
drawRands(g, rands);
g.dispose(); // 结束图像绘制过程,完成图像
// 将图像输出给客户端
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ImageIO.write(image, "JPEG", bos);
byte[] buf = bos.toByteArray();
response.setContentType("image/jpeg");
// 设置浏览器不要缓存此图片
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentLength(buf.length);
// 获取输出流
ServletOutputStream responseOutputStream = response.getOutputStream();
responseOutputStream.write(buf);
bos.close();
responseOutputStream.close();
// 将图像输出给客户端2
// response.setContentType("image/jpeg");
// // 设置浏览器不要缓存此图片
// response.setHeader("Pragma", "No-cache");
// response.setHeader("Cache-Control", "no-cache");
// response.setDateHeader("Expires", 0);
//
// // 获取输出流
// ServletOutputStream responseOutputStream = response.getOutputStream();
// ImageIO.write(image, "JPEG", responseOutputStream);
// responseOutputStream.close();