File download example using servlet and jsp


Environment used:
JDK 1.8
Eclipse Luna
Web module 2.5
Tomcat 8(requires javax.servlet.3.1 jar)

Step 1:
Create an html file with a hyper link to download a file
filedownload.html
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	Click on the link to download:
	<a href="DownloadServlet">Download a File</a>
</body>
</html>

Step 2:
Create a Servlet program
1. Locate the file path
2. Set content type as APPLICATION/OCTET-STREAM
3. Set header as Content-Disposition 

Use attachment in header to download a file or set inline to view in browser
Refer Content-Disposition: https://en.wikipedia.org/wiki/MIME
DownloadServlet.java
package com.candidjava.servlet.download;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		String filename = "home.jsp";
		String filepath = "e:\\";
		response.setContentType("APPLICATION/OCTET-STREAM");
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ filename + "\"");

		// use inline if you want to view the content in browser, helpful for
		// pdf file
		// response.setHeader("Content-Disposition","inline; filename=\"" +
		// filename + "\"");
		FileInputStream fileInputStream = new FileInputStream(filepath
				+ filename);

		int i;
		while ((i = fileInputStream.read()) != -1) {
			out.write(i);
		}
		fileInputStream.close();
		out.close();
	}
}

Step 3:
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Download</display-name>
    <welcome-file-list>
    <welcome-file>filedownload.html</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Downloadservlet</servlet-name>
    <servlet-class>com.candidjava.servlet.download.DownloadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>Downloadservlet</servlet-name>
    <url-pattern>/DownloadServlet</url-pattern>
  </servlet-mapping>
</web-app>

Output Screen shot



Download file upload example
file download using servlet war
file download using servlet zip



Comments

Gayathri Lekshmi
         How to make DownloadServlet.java dynamic?
Kiran
         For the second screenshot displayed, how do I make the screen available in such a way that, only save option is displayed as dialog. It should not contain open with.
Mathan
         Hi Kiran, it depends on browser, code cannot do anything with that..

©candidjava.com