File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Write file to browser tomcat error