File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Downloading file through Servlet. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Downloading file through Servlet." Watch "Downloading file through Servlet." New topic

Downloading file through Servlet.

Siva Vu

Joined: Sep 01, 2004
Posts: 22
Following is the piece of code. fileName is dynamic.
The problem is for smaller files(<1 MB), it is not prompting the dilouge
box to save the file, rather it displayes blank screen. For bigger files, it is prompting and saving the files. IE6.0 browser. Why is it behaving weirdly? Any ideas or any suggestions. Thanks in advance.

FileInputStream fileInput = new FileInputStream(fileName);
int numOfBytes = fileInput.available();
ServletOutputStream stream = response.getOutputStream();
BufferedInputStream fif = new BufferedInputStream(fileInput);
response.setHeader("Content-Type", "application/comma-separated-values; charset=ISO-8859-1");
response.setHeader("Content-Disposition","attachement;filename=" + fileName);
byte[] buffer = new byte[1024];
while (true) {
int bytesRead =, 0, buffer.length);
if (bytesRead < 0)
stream.write(buffer, 0, bytesRead);

Julian Kennedy
Ranch Hand

Joined: Aug 02, 2004
Posts: 823
Cud it bee yor speling?

Easily done.

Siva Vu

Joined: Sep 01, 2004
Posts: 22
Thank you for the reply.

Even after correcting the spelling, it behaves the same way as before.

Does anybody know why it is behaving like this?
Siva Vu

Joined: Sep 01, 2004
Posts: 22
Added to the previous one, when the SAve as dailouge cancelled on the client side, why the file contents are buffered and writes on the browser screen.
Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102
Please post the HTML Link (or button with script coded) which is responsible to download the file from server. Looks like content target is your current document body - that is the reason - my guess.

Did you see the same behavior on IE 5.x or any other browser?
Siva Vu

Joined: Sep 01, 2004
Posts: 22
function download()

document.pageForm.submit_field.value = "H" ;

I am using a button to call this javaScript function,
Servlet pushes created file to client, prompting the user
to save the file. If the user cancels the dailog box, then
the pushed file is buffering(Do not know how it is doing) and
it is prompting to save the file whenever trying to use the
application from the begining(Whenever browser accessing the main page
which is a starting page.)

I am using MSIE 6.0.

Thanks in advance.
[ September 10, 2004: Message edited by: Siva Vu ]
Julia Reynolds
Ranch Hand

Joined: May 31, 2001
Posts: 123

I know this sounds weird, but I had the same problem and solved it by switching the order in which I was setting things in the response header. So try changing from this:

response.setHeader("Content-Type", "application/comma-separated-values; charset=ISO-8859-1");
response.setHeader("Content-Disposition","attachement;filename=" + fileName);

to this:
response.setHeader("Content-Disposition","attachment;filename=" + fileName);
response.setHeader("Content-Type", "application/comma-separated-values; charset=ISO-8859-1");

Good Luck,

Saritha Penumudi
Ranch Hand

Joined: Aug 18, 2003
Posts: 148
I m following this thread and i have a similar requirement where I have to enable client to download a data from the database into a file.

After I get the data I don't want to save that data into a file on the server and then give that file for download but just pass that data over ServletOutputStream and allow user to copy that data in a file on client machine.

I wrote a sample program
response.setHeader("Content-Type", "application/comma-separated-values; charset=ISO-8859-1");

ServletOutputStream stream = response.getOutputStream();
String str ="hello world";
byte[] buffer = new byte[str.length()];
buffer = str.getBytes();
stream.write(buffer, 0, str.length());

with this I am able to open a save/open dialog box onto the broswer and able to save the text in a file but along with the text some exception is also being written into the file.
hello world<PRE>java.lang.IllegalStateException: Response has already been committed
<br>at com.evermind[Oracle Application Server Containers for J2EE 10g (].server.http.EvermindHttpServletResponse.resetBuffer(
<br>at com.evermind[Oracle Application Server Containers for J2EE 10g (].server.http.ServletRequestDispatcher.forward(
<br>at org.apache.struts.action.RequestProcessor.doForward(
<br>at org.apache.struts.action.RequestProcessor.processForwardConfig(
<br>at org.apache.struts.action.RequestProcessor.process(
<br>at org.apache.struts.action.ActionServlet.process(
<br>at org.apache.struts.action.ActionServlet.doGet(
<br>at javax.servlet.http.HttpServlet.service(
<br>at javax.servlet.http.HttpServlet.service(
<br>at com.evermind[Oracle Application Server Containers for J2EE 10g (].server.http.ServletRequestDispatcher.invoke(
<br>at com.evermind[Oracle Application Server Containers for J2EE 10g (].server.http.ServletRequestDispatcher.forwardInternal(
<br>at com.evermind[Oracle Application Server Containers for J2EE 10g (].server.http.HttpRequestHandler.processRequest(
<br>at com.evermind[Oracle Application Server Containers for J2EE 10g (]
<br>at com.evermind[Oracle Application Server Containers for J2EE 10g (]
<br>at com.evermind[Oracle Application Server Containers for J2EE 10g (].util.ReleasableResourcePooledExecutor$

I would appreciate if you help me in getting rid of that extra information.

Thank you
[ September 10, 2004: Message edited by: Saritha ventrapragada ]
Siva Vu

Joined: Sep 01, 2004
Posts: 22
Hi Julia,

Thank you for your input,
still even after changing and playing a little bit with repositioning
of the setHeader lines, the problem persists. The only solution I found
so far is ,shut down the browser and restart the browser, then only
the browser is not asking to store the file or writing the cached results directly in the browser.

Is there any solution, without closing the browser that will prevent the writing or prompting to save the cached results when user cancelled the prompted dialog box?

Thank you
Siva Vu

Joined: Sep 01, 2004
Posts: 22
Any ideas on this problem?
Ajith Anand
Ranch Hand

Joined: Aug 30, 2004
Posts: 40
REF 1:
response.setHeader("Content-Type", "application/comma-seperated-values; charset=ISO-8859-1");
REF 2: response.setHeader("Content-Disposition","attachement;filename=" + filename );

The problem you are facing could be fixed by taking one of the following measures :

In your case, by default, comma-seperated-values are getting associated with IE, maybe because the filename has a known extension : something like .txt or .csv etc. Further these extensions are associated with IE or one of its pluggins.

Solution 1. In windows machines you can configure the file-extensions to Program-associations, by opening the folder options in the control panel.
Here in the control panel , if you are dealing with .txt files goto filetypes tab, choose .txt extensions from the list, and select advanced options, in the dialog, configure open file to "confirm open after download"

But the drawback of this approach is that if you are deploying this thru IE , you cannot go to each and every user and configure their browsers, which leads us to another proposition....

Solution 2. A Hacked Solution - add "xxx" to the file name as below.

REF 2: response.setHeader("Content-Disposition","attachement;filename=" + filename+"XXX" );
This would definitely bring the file save dialog , for IE wouldnt know how to deal with ".txtxxx files.

LXI Technologies P Ltd
I agree. Here's the link:
subject: Downloading file through Servlet.
It's not a secret anymore!