In this article, we will learn how to download files from the server/file directories using JSP. The JSP code has a response header indicating that text is being returned that should be downloaded rather than displayed.
- We will set the content type header to "APPLICATION/OCTET-STREAM", and it will indicate that the content is binary data.
- The FileInputStream object reads the contents of a file which is specified by a file path and file name.
- By using FileInputStream, we can read the contents of the file and pass it directly to the output stream.
- This sends the contents of the file to the client's browser in response.
Implementation to download File using JSP
Below is the Step-by-Step implementation to Download Files using JSP.
Step 1: Prepare sample files.

- File Path: E:/examplefiles
- Sample file 1: sample.pdf
- Sample file 2: sample.txt
Note: Replace the filename and filepath with your filename and filepath in the code.
Step 2: Create a new folder named "filedownload". Create index.html, download-pdf.jsp, download-txt.jsp in the "filedownload" folder and move the folder to tomcat server. Copy and paste the below code in the corresponding files.

Step 3: Start the tomcat server and navigate to the index.html in the browser.
Note: Replace the port number with your port number.
Hit the below URL in browser:
http://localhost:8080/filedownload/index.html
index.html:
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File downloading in JSP</title>
</head>
<body>
<h1 style="color: green;">GeeksforGeeks</h1>
<button>
<a href="download-pdf.jsp">Download sample.pdf file</a>
</button>
<button>
<a href="download-txt.jsp">Download sample.txt file</a>
</button>
</body>
</html>
In the above HTML file, it creates a webpage with two buttons. Each button links to a JSP file (download-pdf.jsp
and download-txt.jsp
) and it is responsible for downloading a corresponding file (sample.pdf
and sample.txt
).
download-pdf.jsp:
HTML
<%
// Define filename and filepath
String filename = "sample.pdf";
String filepath = "E:\\examplefiles\\";
// Set response to download file
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
// Input stream for file reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename);
// Read file content and stream it to the response output stream
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
// Close the FileInputStream to release resources
fileInputStream.close();
%>
The above code downloads a file named "sample.pdf" from "E:\examplefiles\" path. It sets the response headers for file download. Creates a FileInputStream to read the file content and streaming it to the response output stream. At last, it closes the stream.
download-txt.jsp:
HTML
<%
// Define filename and filepath
String filename = "sample.txt";
String filepath = "E:\\examplefiles\\";
// Set response to download file
response.setContentType("APPLICATION/OCTET-STREAM");
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
// Input stream for file reading
java.io.FileInputStream fileInputStream = new java.io.FileInputStream(filepath + filename);
// Read file content and stream it to the response output stream
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
// Close the FileInputStream to release resources
fileInputStream.close();
%>
This JSP code downloads a file named "sample.txt" from "E:\examplefiles\" by setting response headers for file download. Then it creates a FileInputStream to read the file content, and streaming it to the response output stream, ensuring to close the stream at the end.
Output:
Below in the output screen we can see the Files are ready to download.

Then we can download the files as we can see in the below image:
Similar Reads
Downloading Files from FTP with R File Transfer Protocol (FTP) is a standard network protocol for transferring files between a client and server. It is a widely used method for transferring files over the internet from one computer to another. It allows users to upload, download, and manage files on a remote server. FTP is like a fi
9 min read
Downloading Files from Web using Perl Perl is a multi-purpose interpreted language that is often implemented using Perl scripts that can be saved using the .pl extension and run directly using the terminal or command prompt. It is a stable, cross-platform language that was developed primarily with strong capabilities in terms of text ma
9 min read
npm download npm download is a versatile utility that allows you to download and extract files from various sources directly within your Node.js applications. This package is especially useful for programmatically fetching files and handling them within your projects. Below, we will explore the installation proc
2 min read
Spring MVC - Download File Controller The Spring MVC is an approach for designing and developing the web application in the MVC pattern. In this article, we will learn how to develop logic for downloading a file from the server or file location in the Spring Boot with Spring Controller. For the application, we have used one HTML page, S
4 min read
How to Download a File Using Node.js? Downloading files from the internet is a common task in many Node.js applications, whether it's fetching images, videos, documents, or any other type of file. In this article, we'll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries.Using
3 min read
Downloading and Uploading Files from Internet The terms downloading and uploading are generally used while browsing the Internet. Receiving data or a file from the Internet on your computer is referred to as "downloading." Uploading refers to the process of sending data or a file from your computer to a remote location on the Internet. Remote S
7 min read