首页 > 其他分享 >解决getOutputStream() has already been called for this response

解决getOutputStream() has already been called for this response

时间:2023-08-21 17:01:39浏览次数:40  
标签:already bc int getOutputStream been fc jsp response 255


getOutputStream() has already been called for this response异常出现的原因和解决方法:

jsp中出现此错误一般都是在jsp中使用了输出流(如输出图片验证码,文件下载等),没有妥善处理好的原因。

具体的原因:jsp编译成servlet之后在函数

_jspService(HttpServletRequest request, HttpServletResponse response)

 

的最后
有一段这样的代码


1. finally {  
2. if (_jspxFactory != null)   
3.           _jspxFactory.releasePageContext(_jspx_page_context);  
4. }

 

这里是在释放在jsp中使用的对象,会调用response.getWriter(),因为这个方法是和response.getOutputStream()相冲突的!所以会出现以上这个异常。然后当然是要提出解决的办法,其实挺简单的,在使用完输出流以后调用以下两行代码即可:



 


1. out.clear();  
2. out = pageContext.pushBody();

 

最后这里是一个输出彩色验证码例子(这样的例子几乎随处可见)。


  

1. <%@ page  import="java.awt.*,java.awt.image.*,java.util.*,javax.imageio.*" %>  
2. <%@ page import="java.io.OutputStream" %>  
3. <%!  
4. Color getRandColor(int fc,int bc){  
5. Random random = new Random();  
6. if(fc>255) fc=255;  
7. if(bc>255) bc=255;  
8. int r=fc+random.nextInt(bc-fc);  
9. int g=fc+random.nextInt(bc-fc);  
10. int b=fc+random.nextInt(bc-fc);  
11. return new Color(r,g,b);  
12. }  
13. %>  
14. <%  
15. try{  
16. response.setHeader("Pragma","No-cache");  
17. response.setHeader("Cache-Control","no-cache");  
18. response.setDateHeader("Expires", 0);  
19. int width=60, height=20;  
20. BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
21. OutputStream os=response.getOutputStream();  
22. Graphics g = image.getGraphics();  
23. Random random = new Random();  
24. g.setColor(getRandColor(200,250));  
25. g.fillRect(0, 0, width, height);  
26.   
27. g.setFont(new Font("Times New Roman",Font.PLAIN,18));  
28. g.setColor(getRandColor(160,200));  
29. for (int i=0;i<155;i++)  
30. {  
31. int x = random.nextInt(width);  
32. int y = random.nextInt(height);  
33. int xl = random.nextInt(12);  
34. int yl = random.nextInt(12);  
35. g.drawLine(x,y,x+xl,y+yl);  
36. }  
37. String sRand="";  
38. for (int i=0;i<4;i++){  
39. String rand=String.valueOf(random.nextInt(10));  
40. sRand+=rand;  
41. g.setColor(new Color(20+random.nextInt(110),20+random.nextInt(110),20+random.nextInt(110)));  
42. g.drawString(rand,13*i+6,16);  
43. }  
44. session.setAttribute("rand",sRand);  
45. g.dispose();  
46.   
47. ImageIO.write(image, "JPEG",os);  
48.   
49. //注意看以下几句的使用  
50. os.flush();  
51. os.close();  
52. os=null;  
53. response.flushBuffer();  
54. out.clear();  
55. out = pageContext.pushBody();  
56. }  
57. catch(IllegalStateException e)  
58. {  
59. System.out.println(e.getMessage());  
60. e.printStackTrace();  
61. }%>

 

标签:already,bc,int,getOutputStream,been,fc,jsp,response,255
From: https://blog.51cto.com/u_16230604/7177306

相关文章

  • 安装MinGW时报错“The file has been downloaded incorrectly!”解决方案
    前言作为一个准备学习C语言的准大一的通信工程崽子,Linux还不会用,在Windows上使用gcc得安装MinGW。我知道各位大佬们看到第一句话之后想说些什么。虽然说有很多优秀的编程软件自带了MinGW,但是想想,如果使用Windows的cmd直接敲个“gcc-v”来跑代码,逼格不直接蹭蹭上涨,能够充分满足......
  • Redundant declaration: @SpringBootApplication already applies given @ComponentSc
    报错提示内容: 解决:将启动类文件移动到com.atguigu.eduservice包。应该是EduApplication.java文件自带的@SpringBootApplication中包含@ComponentScan,默认是扫描该类所在的包和子包的,即@ComponentScan(basePackages={"com.atguigu"}),所以再写一遍就提示多余的。 ......
  • ethereum错误之already known
    根据提供的错误信息error(*github.com/ethereum/go-ethereum/rpc.jsonError)*{Code:-32000,Message:"alreadyknown",Data:interface{}nil},这是一个来自以太坊的JSON-RPC错误。该错误的含义是“alreadyknown”,即“已经存在”。在以太坊中,当您尝试发送一个已经存在于区......
  • bazel 使用 gtest/gmock 报错 Constraints from @bazel_tools//platforms have been r
    问题描述运行bazeltest命令,遇到错误:“Constraintsfrom@bazel_tools//platformshavebeenremoved.Pleaseuseconstraintsfrom@platformsrepositoryembeddedinBazel,orpreferablydeclaredependencyonhttps://github.com/bazelbuild/platforms.Seehttps://gi......
  • Port XXX is already in use. xxxx..解决办法-gradio退出可用
    原因:端口被占用,程序启动后关闭但端口依然存在解决办法:手动杀死端口  1.安装工具(已经有的不需要安装,直接跳到第二步)yuminstallnet-tools-y命令介绍:yum:自动化简单化地管理rpm包的命令。install:安装net-tools:网络工具 2.安装完毕,执行查看端口命令: netstat-tpln......
  • SSRS 2016 DeviceInfo Name already exists Parameter name: deviceInfoName
    https://learn.microsoft.com/en-us/answers/questions/784851/ssrs-2016-deviceinfo-name-already-exists-parameterhttps://social.msdn.microsoft.com/Forums/sqlserver/en-US/5b4acc6d-058b-4c40-b916-cc634bb35f61/ssrs-2012-deviceinfo-name-already-exists-parmeter-n......
  • Uncaught SyntaxError: Identifier 'originalPrompt' has already been declared
    控制台报错:UncaughtSyntaxError:Identifier'originalPrompt'hasalreadybeendeclared网上查询相关资料,预测是GoogleChrome浏览器安装了插件跟Vue项目运行代码出现了冲突。解决方法:关闭相关插件即可,【可能导致问题产生的插件有:SeleniumIDE】。参考文档:http://www.dtm......
  • xposed优秀模块列表 --- trustmealready
    https://repo.xposed.info/module/com.virb3.trustmealready一个Xposed模块,使用MattiaVinci提供的出色技术在Android上禁用SSL验证和固定。效果是系统范围的。适用于各种安全审计。 作者: ViRb3支持/讨论网址: https://github.com/ViRb3/TrustMeAlready源代码网址: https......
  • The Report Viewer Web Control HTTP Handler has not been registered in the applic
     system.webServer内加<handlers><addname="ReportViewerWebControlHandler"preCondition="integratedMode"verb="*"path="Reserved.ReportViewerWebControl.axd"type="Microsoft.Reporting.WebForms.Htt......
  • stm32cubeide ioc报错 This IOC file has been generated with CubeMX version 5.6.1
    STM32Cubemx文件的版本不一致导致打不开.ioc文件的问题问题:ThisIOCfilehasbeengeneratedwithCubeMXversion5.6.1YourcurrentCubeMXversionis5.0.0PleaseupdatetoanewestCubeMXversiontobeabletoopenthisIOC.笔者遇到这个问题后,就开始升级程序,但是升级......