生成验证码页面 报 java.lang.IllegalStateException: getOutputStream() has already been called 解决办法
来源:互联网 发布:mysql 分组之后排序 编辑:程序博客网 时间:2024/06/14 00:40
生成验证码页面核心代码:
/* * 绘图 *///step1:创建一个内存映像(画布)BufferedImage image = new BufferedImage(100,30,BufferedImage.TYPE_INT_RGB);//step2:获得画笔Graphics g = image.getGraphics();//step3:给画笔设置颜色g.setColor(new Color(255,255,255));//step4:设置画布的背景g.fillRect(0, 0, 100, 30);//step5:生成一个随机数Random r = new Random();String number = getNumber(5);System.out.println(number);session.setAttribute("myzjcode", number);//step6:将随机数绘制到图片for(int i=0;i<5;i++){String randNumStr = number.substring(i,i+1);g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));//g.setFont(new Font(null,Font.ITALIC,24)); g.setFont(new Font("Times New Roman",Font.BOLD,24));g.drawString(randNumStr, 5+18*i, 25);}//g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));//g.setFont(new Font(null,Font.ITALIC,24));//g.drawString(number, 5, 25);//step7:加一些干扰线for(int i=0;i<8;i++){g.setColor(new Color(r.nextInt(255),r.nextInt(255),r.nextInt(255)));g.drawLine(r.nextInt(100), r.nextInt(30), r.nextInt(100), r.nextInt(30));}/* * 压缩图片,然后输出 *///step1:设置服务器返回的数据类型为图片out.clear(); out = pageContext.pushBody(); response.setContentType("image/jpeg");//step2:获得输出流(使用字节输出流)OutputStream os = response.getOutputStream();//step3:压缩图片并输出ImageIO.write(image, "jpeg", os);//step4:关流os.close();
问题:未添加
out.clear();
out = pageContext.pushBody();
之前报java.lang.IllegalStateException: getOutputStream() has already been called
原因分析:
产生这样的异常原因:是web容器生成的servlet代码中有out.write(""),
这个和JSP中调用的response.getOutputStream()产生冲突.即Servlet规范说明,
不能既调用response.getOutputStream(),又调用response.getWriter(),
无论先调用哪一个,在调用第二个时候应会抛出IllegalStateException,因为在jsp中,
out变量实际上是通过response.getWriter得到的,你的程序中既用了response.getOutputStream,又用了out变量,故出现以上错误。
解决办法:
在调用第一个之后,调用第二个之前,添加
out.clear();
out = pageContext.pushBody();
阅读全文
0 0
- 生成验证码页面 报 java.lang.IllegalStateException: getOutputStream() has already been called 解决办法
- java.lang.IllegalStateException: getOutputStream() has already been called 解决办法
- 验证码异常:java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called
- java.lang.IllegalStateException: getOutputStream() has already been called for t
- java.lang.IllegalStateException: getOutputStream() has already been called 解决方法
- java.lang.IllegalStateException: getOutputStream() has already been called f
- java.lang.IllegalStateException: getOutputStream() has already been called f
- java.lang.IllegalStateException: getOutputStream() has already been called for t
- java.lang.IllegalStateException: getOutputStream() has already been called for this response的解决办法
- java.lang.IllegalStateException: getOutputStream() has already been called for this respons解决办法
- 文件能下载,但是报错:java.lang.IllegalStateException:getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response,java.la
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException:getOutputStream() has already been called for this response的解决方法
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- java.lang.IllegalStateException: getOutputStream() has already been called for this response
- js判断linux服务器的图片是否存在
- js中的事件委托或是事件代理详解
- 数据结构实验之图论七:驴友计划
- Hadoop中的join
- 手动将经典 VM 从 VHD 迁移到新的 ARM 托管磁盘 VM
- 生成验证码页面 报 java.lang.IllegalStateException: getOutputStream() has already been called 解决办法
- mysql 修改忘记root密码
- 关于手机键盘挡住输入框的问题
- Android 7.0 版本更新
- linux 子目录赋权限
- qt4 Q_EXPORT_PLUGIN2 到qt5 Q_PLUGIN_METADATA变化
- gstreamer插件指南
- java程序员从笨鸟到菜鸟之(四十)I/O流
- CAD文件怎么打印成黑白图片教程