输出流问题
我已阅读 Servlet 异常 - getOutputStream() 已经已要求此回复,但我没有找到任何解决我的问题的方法。
在我的 main.jsp 中,我有这样的语句:
<img src="ImageElaborator.jsp" style="float: left" alt="" height="70" width="70"/>
在我的 ImageElaborator.jsp 中:
byte[] photo = getPhoto();
response.getOutputStream().write(photo, 0, photo.length);
此代码片段显示了我的图像,但我有此错误:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
我不明白如何解决它。请帮忙!
I've read Servlet Exception - getOutputStream() has already been called for this response but I didn't found any solution for my problem.
In my main.jsp I've this statement:
<img src="ImageElaborator.jsp" style="float: left" alt="" height="70" width="70"/>
and in my ImageElaborator.jsp:
byte[] photo = getPhoto();
response.getOutputStream().write(photo, 0, photo.length);
This snippet shows my image, but I've this error:
java.lang.IllegalStateException: getOutputStream() has already been called for this response
I don't understand how I can solve it. Please help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要在 JSP 中执行此操作。在 servlet 中执行此操作。
Don't do this in a JSP. Do it in a servlet.
你不应该把这样的代码放在JSP中,因为在执行代码时,JSP开头的一些空格、换行符等可能已经被发送到JSP的输出流中。
JSP 应用于输出文本或标记,但不用于执行业务逻辑并将原始字节发送到输出流。您应该将此类代码放在 servlet 中,或者放在您最喜欢的 MVC 框架(Struts、Stripes、Spring MVC 等)的操作中
You shouldn't put such code in a JSP, because at the time the code is executed, some blank spaces, newlines, etc. at the beginning of the JSP have probably already been sent to the output stream of the JSP.
JSP should be used to output text or markup, but not to execute business logic and send raw bytes to the output stream. You should put this kind of code in a servlet, or in an action of your favorite MVC framework (Struts, Stripes, Spring MVC, etc.)
在 JSP 中,您不应该调用 OutputStream,因为它被定义为隐式变量
查看隐式会话和对象:
http://www.exforsys.com/tutorials/jsp /jsp-implicit-and-session-objects.html
我认为类似的东西应该没问题:
但最好的方法是使用 Servlet,正如人们所说的那样。
In a JSP you're not supposed to call the OutputStream as it is defined as implicit variable
see implicit session and objects:
http://www.exforsys.com/tutorials/jsp/jsp-implicit-and-session-objects.html
I reckon something like that should be OK:
But the best way to do is using a Servlet as it has been said.