Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Downloading a file via JSP

843836May 9 2005 — edited May 9 2005
I am trying to write some code which will generate a file and then download the file
from my Webserver. When I download the file I get "Error 500: OutputStream already obtained"
appended to the file. I can't work out why this is so.

Here is the code I have used to download the file.
I have also included the file before and after transmission.
Any help would be appreciated.

<%@page import="java.net.*,javax.servlet.http.*,javax.servlet.*,java.io.*" %>
<%
ServletOutputStream out1 = response.getOutputStream();

String fileName = (String)request.getParameter("filename");
// MIME type for pdf doc
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename="+fileName+";");
File f = new File("\\"+fileName);
FileInputStream stream = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(stream);
BufferedOutputStream bos = new BufferedOutputStream(out1);

byte[] buff = new byte[2048];
int bytesRead;

// Simple read/write loop.
try {
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
if (bos != null) bos.close();
e.printStackTrace();
}
finally {
}
if (bis != null) bis.close();
if (bos != null) bos.close();
if (stream != null) stream.close();
%>

This is the file on the WebServer

The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy Dog


This is the file after it has been transferred

The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy Dog
The Quick Brown Fox Jumps Over The Lazy DogError 500: OutputStream already obtained


Thanks

Adrian Campbell
Honda Australia
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jun 6 2005
Added on May 9 2005
2 comments
214 views