jsp 图片 验证码 getOutputStream() has already been called for this response 问题的解决

在写JSP程序的时候,如果程序中调用了response.getOutputStream()去向客户端输出文件等数据流,容器就会抛出这样的异常:
Java.lang.IllegalStateException: getOutputStream() has already been called for this response
at org.apache.catalina.connector.Response.getWriter(Response.java:596)
at org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:186)
产生这样的异常原因:是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();

就可以了。

<%@ page contentType="image/jpeg" import="java.io.OutputStream,java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*,com.sun.image.codec.jpeg.JPEGCodec,com.sun.image.codec.jpeg.JPEGImageEncoder;"%><%!
Color getRandColor(int fc,int bc){//
  Random random = new Random();
  if(fc>255) fc=255;
  if(bc>255) bc=255;
  int r=fc+random.nextInt(bc-fc);
  int g=fc+random.nextInt(bc-fc);
  int b=fc+random.nextInt(bc-fc);
  return new Color(r,g,b);
}
%><%
response.reset();
//
response.setHeader("Pragma","No-cache");
response.setHeader("Cache-Control","no-cache");
response.setDateHeader("Expires", 0);
//
int width=60, height=20;
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = image.getGraphics();
Random random = new Random();
g.setColor(getRandColor(200,250));
g.fillRect(0, 0, width, height);
g.setFont(new Font("Times New Roman",Font.PLAIN,18));
//g.setColor(new Color());
//g.drawRect(0,0,width-1,height-1);
g.setColor(getRandColor(160,200));
for (int i=0;i<155;i++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(12);
int yl = random.nextInt(12);
g.drawLine(x,y,x+xl,y+yl);
}
//
String sRand="";
for (int i=0;i<4;i++){
String rand=String.valueOf(random.nextInt(10));
sRand+=rand;
//
g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));
//
g.drawString(rand,13*i+6,16);
}
//
request.getSession().setAttribute("checkcode",sRand);
//
g.dispose();
//
OutputStream output=response.getOutputStream();
response.flushBuffer();
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
encoder.encode(image);
out.clear();
out = pageContext.pushBody();%>
 

相关文章:
JSP彩色验证码,javax.imageio.IIOException: Can&apos;t create output stream! 解决方法
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当你在项目中进行数据导出到Excel时,你可能会遇到"getOutputStream() has already been called for this response"的异常。这个异常的原因可能是因为在你的代码中,同时调用了response.getOutputStream()和response.getWriter(),而根据Servlet规范,这两个方法不能同时调用。 在你提供的代码中,我可以看到你使用了@GetMapping注解来处理导出功能,并在方法参数中传入了HttpServletResponse对象。然后,你调用了ExcelPortUtil.excelPort()方法来进行数据写入Excel文件并返回。 为了解决"getOutputStream() has already been called for this response"异常,你可以按照下面的步骤进行调整: 1. 确保在你的代码中只调用response.getOutputStream()或response.getWriter()其中之一。 2. 检查你的ExcelPortUtil.excelPort()方法内部的实现,确保没有调用response.getOutputStream()或response.getWriter()。 3. 如果你的ExcelPortUtil.excelPort()方法内部确实需要使用response.getOutputStream(),那么你需要修改你的方法逻辑,确保在调用response.getOutputStream()之前没有调用response.getWriter()。 通过以上调整,你应该能够解决"getOutputStream() has already been called for this response"的异常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [getOutputStream() has already been called for this response问题终极解决](https://blog.csdn.net/IT_51888_liang/article/details/122235797)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值