|
Java学習者论坛
在JSPzhong使用下Mian的代碼来生成验证码图像 但shitomcat報错:
2000-02-05 22:35:38 StandardWrapperValve[jsp]: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: getOutputStream() has already been called for this response
源程序代码:
<%@ page contentType="image/jpeg" import="java.awt.,java.awt.image.,java.util.*,javax.imageio.*" %>
<%
response.setHeader("ragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
int width=100, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
g.setColor(Color.yellow);
g.fillRect(0, 0, width, height);
g.setColor(Color.red);
g.drawRect(0,0,width-1,height-1);
String rand = "";
Random ram = new Random();
String[] strRand = new String[]{"0","1","2","3","4","5","6","7","8","9"};
for(int i=0;i<4;i++)
{
rand += strRand[ram.nextInt(strRand.length)];
}
session.setAttribute("rand",rand);
g.setColor(Color.black);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
g.drawString(rand,28,15);
g.dispose();
ImageIO.write(image, "JPEG", response.getOutputStream());
%>
欢迎来到Java学习者論坛,zhuan载请注明地址:http://www.javaxxz.com. |
|