How to force -download an attachment/application using JSP.

Submitted on: 1/2/2015 12:23:00 PM
By: Manjunath P Reddy (from psc cd)  
Level: Advanced
User Rating: By 11 Users
Compatibility: Java (JDK 1.1), Java (JDK 1.2)
Views: 1823
     This article along with the code snippet illustrates how to force download an attachment from the server to clients using JSP.

How to force download of an attachment/application using JSP.

Many of you guys out there would have experienced the problems associated with the browser interpreting whether to download an application/attachment or open up in the same browser instance. Whenever microsoft's IE encounters a href tag pointing to an excel/doc/ppt or any other tool whose plugins have been installed on the client, it opens the attachment in the same instance of the browser. Hence if a user has to save the file then he has to be smart enuff to click on file --> save as from the menu and then save to his hard disk.

But most web-applications are designed for ease and not keeping the user's technical skill or knowledge in view. Hence when a user clicks on a href(which might say click to download pdf version) pointing to say mydownloadable.pdf, what happens is the pdf downloads the file to his temporary internet files and shows it in the browser.

The code snippet below shows how one can force download a file/app/attachment to the user irrespective of whether the user has a necessary plugin or not. Even the name of the downloaded file can be specified dynamically.

<!--contents of download.jsp-->
<%@ page import="java.util.*,*"%>
<!--Assumes that file name is in the request objects query Parameter -->
	//read the file name.
	File f = new File ("c:/fop/mypdf/" + request.getParameter("file") );
	//set the content type(can be excel/word/powerpoint etc..)
	response.setContentType ("application/pdf");
	//set the header and also the Name by which user will be prompted to save
	response.setHeader ("Content-Disposition", "attachment; 				
	//get the file name
	String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
	//OPen an input stream to the file and post the file contents thru the 
	//servlet output stream to the client m/c
		InputStream in = new FileInputStream(f);
		ServletOutputStream outs = response.getOutputStream();
		int bit = 256;
		int i = 0;
		try {
			while ((bit) >= 0) {
				bit =;
			//System.out.println("" +bit);
		} catch (IOException ioe) {
//		System.out.println( "\n" + i + " bytes sent.");
//		System.out.println( "\n" + f.length() + " bytes sent.");

Hope you find this useful...
Adios and Happy Programming!!!
Carved upon my stone, My body lies but still I roam...
Manjunath P Reddy

Other 3 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.