Java Mailing List Archive

http://www.junlu.com/

Home » Home (12/2007) » Tomcat Users »

[OT] avoiding getOutputStream() being called twice

Robert Priest

2004-02-05

Replies:

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@(protected)
For additional commands, e-mail: tomcat-user-help@(protected)


©2008 junlu.com - Jax Systems, LLC, U.S.A.