输出流问题

发布于 2024-10-17 05:53:05 字数 669 浏览 2 评论 0原文

我已阅读 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一个人的夜不怕黑 2024-10-24 05:53:05

不要在 JSP 中执行此操作。在 servlet 中执行此操作。

Don't do this in a JSP. Do it in a servlet.

爱已欠费 2024-10-24 05:53:05

你不应该把这样的代码放在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.)

坏尐絯℡ 2024-10-24 05:53:05

在 JSP 中,您不应该调用 OutputStream,因为它被定义为隐式变量
查看隐式会话和对象:
http://www.exforsys.com/tutorials/jsp /jsp-implicit-and-session-objects.html

我认为类似的东西应该没问题:

byte[] photo = getPhoto();
out.write(photo, 0, photo.length);

但最好的方法是使用 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:

byte[] photo = getPhoto();
out.write(photo, 0, photo.length);

But the best way to do is using a Servlet as it has been said.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文