my dog learned polymorphism*
The moose likes Servlets and the fly likes Downloading PDF files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Downloading PDF files " Watch "Downloading PDF files " New topic

Downloading PDF files

vandana murthy

Joined: Dec 20, 2000
Posts: 4
I have to download a pdf file and am using Servlets and xml, xsl.
I am facing a very peculiar problem, I am able to download the file in netscape v 4.73, but not in IE v 5.0.
The sample code is as mentioned below:
OutputStream outStream = response.getOutputStream();
FileInputStream fileInput = new FileInputStream(fileName);
int numOfBytes = fileInput.available();
byte byteArray[] = new byte[numOfBytes];
int nextByte =;
OutputStream outStream = response.getOutputStream();
Note: here response is HttpServletResponse.
I have changed the setContentType to application/x-filler & application/octet-stream. These two work fine in IE but donot work in netscape. In netscape the downloaded file is saved as .html.
When I retain the contentType as application/pdf, the IE download goes into a loop and hangs.
CAN ANYBODY HELP ME. I am in a very crucial stage of my project. Any help in this regard would be appreciated.
PS: Kindly feel free to reply to me regarding any other method by which I can download a pdf file using servlets.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12761
It might help to also set the content length
response.setContentLength( numOfBytes );
that way the browser knows when it has the entire document.

author of:
vandana murthy

Joined: Dec 20, 2000
Posts: 4
Hi Bill,
Thanks for the help, but to my utter dismay, setting the content length has not helped.
If I keep the content type as application/pdf, The servlet is being called more than once if I use IE, but netscape does it only once. In case the content type is application/x-filler, the servlet is called once, but download is not possible.
Is there any logical reason why a servlet should be called more than once if I click a link in IE which calls this servlet?
PS: I am using Apache 1.3 as my web server.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12761
This sure sounds weird. Are you sure your MSIE browser is set up to understand the application/pdf MIME type? (Grasping at straws here)
Nitin Dubey
Ranch Hand

Joined: Oct 30, 2000
Posts: 126

I want to upload the file to server and download the file to the HTML client. I m using servlets.
As I think some of you people know this thing please help me.
vandana murthy

Joined: Dec 20, 2000
Posts: 4
Hope this helps you...
To upload a file you can use the MultipartRequest class.
Create an instance of MultipartRequest class. Set maximum file size to whatever you want
multiPart=new MultipartRequest(request,saveDirectory,0x100000);
// Here
// saveDirectory = the url of the directory where you want to save the file
// request = HttpServletRequest
// 0x100000 = the post size, you could substitute this based on your own file size
For this code to work, you need to import com.oreilly.servlet.*; which is available in cos.jar. You could download this jar file from the net.

To download a file you can set the content type in your servlet as setContentType = application/x-filler or application/octet-stream or application/pdf etc (there are many content types).
If you are using just an html page where you need to download, you could also give the file name as a post data, i.e, after the
Sunil Madipalli

Joined: May 01, 2000
Posts: 2
I am having the exact same problem where IE calls the servlet multiple times, but it only happens once with Netscape. Fortunately for me, IE seems to call only 3 times, it does not go into a loop.
Let us know when you find a solution.
[This message has been edited by Sunil Madipalli (edited February 01, 2001).]

Beksy Kurian
Ranch Hand

Joined: Jul 11, 2001
Posts: 254
did any of you find a solution for IE problem. I am also having the same problem with IE 5.01.
I agree. Here's the link:
subject: Downloading PDF files
Similar Threads
File download with servlets
Downloading file from server
Outputstream, IllegalStateException
Servlet download file writer is already used by this request
How to pass 'Save as' for download?