Java Servlet File Download
By AmarSivas | | Updated : 2021-03-12 | Viewed : 8769 times
This current tutorial provides a neat example for file download with a detailed explanation.

Perquisitions
-
JdK 11
-
Servlet 4.0
-
Apache-Tomcat 9.0.16
-
Eclipse 4.X
-
Apache Maven 3.X
Click on File tab
--> New
--> Click on Maven Project
--> Please check on Create Simple Project (Skip architype selection)
--> Click on Next --> Enter the values com.docsconsole.tutorials.servlet4 as Group Id, FileDownloadApp-Example as Artifact Id
--> Click on Finish
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.docsconsole.servlet4.tutorials</groupId>
<artifactId>FileDownloadApp-Example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>FileDownloadApp-Example</name>
<description>FileDownloadApp-Example</description>
<properties>
<maven.war.plugin.version>3.2.2</maven.war.plugin.version>
<servlets.version>4.0.0</servlets.version>
<jsp.version>2.3.1</jsp.version>
<jstl.version>1.2.2</jstl.version>
<tld.version>1.1.2</tld.version>
<log4j.version>1.2.17</log4j.version>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlets.version}</version>
<scope>provided</scope>
</dependency>
<!-- JSP Dependency -->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- JSTL Dependency -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>javax.servlet.jsp.jstl-api</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- TagLibs Dependency -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>${tld.version}</version>
</dependency>
<!-- Log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>${maven.war.plugin.version}</version>
<configuration>
<warSourceDirectory>src/main/webapp</warSourceDirectory>
<warName>${project.artifactId}</warName>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
Please create the Java, xml and JSP files as given follow.
package com.docsconsole.servelet.tutorials;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
public class DownloadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
String filename = "Document.docx";
String filepath = "D:\\AllWorkspaces\\";
response.setContentType("application/msword");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
FileInputStream fileInputStream = new FileInputStream(filepath + filename);
int i;
while ((i = fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();
System.out.println("File has been downloaded successully...");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>FileDownloadApp-Example</display-name>
<servlet>
<servlet-name>DownloadServlet</servlet-name>
<servlet-class>com.docsconsole.servelet.tutorials.DownloadServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DownloadServlet</servlet-name>
<url-pattern>/fileDownload</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>home.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>FileUploadApp Example</title>
</head>
<body>
<div>
<h3> Download File: </h3>
Download file:<a href="fileDownload">Download File</a>
</div>
</body>
</html>
The final directory of the project will be as given below.

Before the executing this application, you need to put the fie Document.docx in the directory D:\\AllWorkspaces\\ (i.e., you can change directory in windows OS and modify the location in java file).
After deployment of war, hit the URL “http://localhost:8080/FileDownloadApp-Example/” and verify the file is downloaded or not.

Please refer to the Github repository here Servlet-FileDownloading-Example-App