It's not a secret anymore!
The moose likes Servlets and the fly likes Write file to browser tomcat error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Write file to browser tomcat error" Watch "Write file to browser tomcat error" New topic

Write file to browser tomcat error

Sue Stephen

Joined: May 04, 2004
Posts: 11
I am using the code below in a servlet to write a jar file to the browser so that user can download it. However, after the user hit download to save the file, the tomcat server display an error caused by flushing the buffer. Does anyone know why it happens? Thank you for your help

String disHeader = "Attachment;Filename=\"" + jarName +"\"";
response.setHeader("Content-Disposition", disHeader);

FileInputStream fileInputStream = new FileInputStream(jarFile);

int i;
while ((!=-1)
out.close(); //This line gives the problem

catch(Exception e) // file IO errors
System.out.println("######### file error");


org.apache.jasper.runtime.PageContext: Internal error flushing the buffer in release()
Sue Stephen

Joined: May 04, 2004
Posts: 11
Sorry, it actually is a jsp page to write to the browser.
James Carman
Ranch Hand

Joined: Feb 20, 2001
Posts: 580
The servlet engine will close the output stream for you automatically. This sort of logic should go in a servlet of course.

James Carman, President<br />Carman Consulting, Inc.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Also, the "out" object is a printWriter which is meant for writing text streams not binary files. For that you will need a servletOutputStream.

And I agree, you don't want to do this from a JSP.

Why can't you just provide a link to it?

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: Write file to browser tomcat error
jQuery in Action, 3rd edition