This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Write file to browser tomcat error" Watch "Write file to browser tomcat error" New topic
Author

Write file to browser tomcat error

Sue Stephen
Greenhorn

Joined: May 04, 2004
Posts: 11
Hi,
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

-------------------------------------------------------
try{
String disHeader = "Attachment;Filename=\"" + jarName +"\"";
response.setHeader("Content-Disposition", disHeader);
response.setContentType("APPLICATION/OCTET-STREAM");

FileInputStream fileInputStream = new FileInputStream(jarFile);

int i;
while ((i=fileInputStream.read())!=-1)
{
out.write(i);
}
fileInputStream.close();
out.close(); //This line gives the problem
}

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

error:

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

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
Sheriff

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
 
wood burning stoves
 
subject: Write file to browser tomcat error
 
Similar Threads
error in dwonloading a pdf file
Unable to upload file
how to save the file in clint side in temp folder
Problem in downloading file from server to client
how to use jar untility in java program to jar files?