You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Robert Priest <Ro...@bentley.com> on 2004/02/05 16:22:36 UTC
[OT] avoiding getOutputStream() being called twice
Hello,
How can I pass a byte[] down to my client, without calling
"response.getOutputStream" explicitly?
Here is my problem:
I want to pass a file down to the client via a jsp page.
So I want to write the file as bytes, not chars. So i do a:
BufferedOutputStream reqOut = new
BufferedOutputStream(response.getOutputStream());
// send file to browser
if (mClientFile.exists())
{ FileInputStream fr = new FileInputStream(mClientFile);
BufferedInputStream is =new BufferedInputStream(fr);
response.setContentType("application/octet-stream");
response.setContentLength((int)mClientFile.length());
byte[] buf = new byte[32 * 1024]; // 32k buffer
int nRead = 0;
while( (nRead=is.read(buf)) != -1 ) {
reqOut.write(buf, 0, nRead);
}
reqOut.flush();
}
// end
reqOut.close();
Which works for me. However, I am in danger of getting the
"getOutputStream() called twice" error. Doesn't always happen, but sometimes
it does. I don't want to use the JspWriter "out" because writers use
character streams. And everytime I use a character stream my files end up
corrupted when I pass a file that has "unicode" characters in it.
So my solution is to just call getOutputStream() and pass the file as bytes.
Some of the files that I will pass down are binary, so I don't think I
should be using character streams anyway.
Anyone have a better solution that can avoid the aforementioned problem with
"getOutputStream() called twice" ?
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org
Re: [OT] avoiding getOutputStream() being called twice
Posted by Philipp Taprogge <Ph...@gmx.net>.
Hi!
Robert Priest wrote:
> How can I pass a byte[] down to my client, without calling
> "response.getOutputStream" explicitly?
>
> Here is my problem:
>
> I want to pass a file down to the client via a jsp page.
At first glance, I can't tell you why you get this error, but IMHO you
could save yourself a lot of trouble if you used a servlet directly in
this case. A JSP is a bad choice here, I think, since a JSP response
is presumed to have text (or html rather) content. I think your
problem might be that some jsp header information is already sent to
the client using the jspwriter when you call the getOutputStream() method.
You should decide what you want. If you want the client to display
something, send text, if you want to push a file to the client, send
bytes, but not both in the same reply.
In such cases I use a servlet, that first sends the binary data to the
client and then sendRedirect()s the client to a jsp that displays the
outcome of the operation.
Phil
---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org