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

Download option from Tomcat

Kalyan Anand
Ranch Hand

Joined: Feb 07, 2007
Posts: 194
I have a html where I used href to point pdfs... these pdfs are placed under webpplication folder in tomcat.

when the user clicks on this href link... the pdf content is shown in the browser with some junk chars.... all I want is when user clicks on the href... it should ask the user to save/open dialog.. the option which we generally see on any website in downloading pdfs.

any help ?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2904

Try to stream PDF from Servlet.

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
The SimpleStream example in the shows how to stream binary files from a servlet.

You'll also need to set the Content-Disposition header as an attachment with a file name; something like:
response.setHeader("Content-Disposition", "attachment; filename=\"foobar.pdf\"");
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17421

Also, don't store writeable files (PDF or otherwise) in the web application folder. It can cost you significantly. Read-only files are fine, but if you upload files into the webapp directory, it won't always work and you may end up like I did and get zapped when someone upgrades the webapp and the data files are get erased.

An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: Download option from Tomcat
It's not a secret anymore!