Spring MVC - Download File Controller
Last Updated :
24 Apr, 2025
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, Spring Boot with MVC Pattern. We will explain from scratch how to develop this logic in Spring Boot with Spring MVC pattern.
Prerequisites
To understand this application, we need basic knowledge of the below technologies:
- Spring Boot
- Thymeleaf
- Spring MVC
- Bootstrap Framework
Here we have used Spring Boot for developing the main logic for file download and Bootstrap Framework is used for creating a good web interface for downloading the file.
Project Steps
- Step 1: Create a Spring Stater Project using your favorite IDE (Reference)
- Step 2: In the main package, create one Java class for the Download File Controller
- Step 3: After that create one HTML file in Templates which is located in the Resource folder of the same folder.
- Step 4: After developing the logic, run this project as a Spring boot App.
- Step 5: After that open your browser then type this http://localhost:8080/
- Step 6: After that, you have one HTML interface on that page one button is there click on it.
Then file will be downloaded into your system.
Project Folder Structure

Now we will discuss the main logic for downloading the file with Spring Boot with Controller layer. After that we will see the testing with images in the below.
Download File Controller:
Java
package com.gfg.articles;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import jakarta.servlet.http.HttpServletResponse;
@Controller
public class FileDownloadController {
@GetMapping("/download")
public void downloadFile(HttpServletResponse response) {
try {
// Load file as Resource
Resource resource = new ClassPathResource("demo.txt");
// Set response content type
response.setContentType("text/plain");
// Set response headers
response.setHeader("Content-Disposition", "attachment; filename=" + resource.getFilename());
// Get input stream from the file resource
InputStream inputStream = resource.getInputStream();
// Get output stream of the response
OutputStream outputStream = response.getOutputStream();
// Copy input stream to output stream
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
// Close streams
inputStream.close();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
In the above code, first we have created one GET mapping for download the file, after that we provided the path of the downloaded file from the location or file server. Once we got file location then we will set content type as text. We have written this entire logic within try catch block. If file is not existed, then it throws an exception otherwise File is downloaded in your local System. Here we have used HttpServletResponse for getting response from the API Hit point.
HTML Content:
HTML
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>File Download Example</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body class="container mt-5">
<h2 class="mb-4">File Download Example</h2>
<a th:href="@{/download}" class="btn btn-success">Download Sample File</a>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
Here we have created one HTML file. With that File we have created one anchor tag, by using Thymeleaf we hit the GET mapping API point. Then back-end logic is worked which is write in controller layer. Then file will be downloaded. Below we have provided the output images for better understanding.
Output:

When you click on the Download Sample File Button. If the file exists in the given location, then the file will be downloaded, otherwise It will show exception.
Conclusion
File Download concept is widely used in every software application for gathering information from users. In this article, we have provided the basic example for how a file will be downloaded while click on given link or button. We developed this logic in controller layer. For understanding this example, you need basic knowledge on spring boot with Spring MVC pattern. In the Above example, we have mentioned the file location for as Resource you observe in the Controller java file.
Similar Reads
Spring MVC - Multiple Controller
We may construct numerous controllers at once in Spring MVC. Each controller class must be annotated with the @Controller annotation. A Spring MVC example with numerous controllers can be found here. The procedure is as follows: In the case of Maven, load the spring jar files or add dependencies.Mak
2 min read
Spring - REST Controller
Spring Boot is built on top of the Spring and contains all the features of spring. Spring Boot is a popular framework for building microservices and RESTful APIs due to its rapid setup and minimal configuration requirements. When developing REST APIs in Spring, the @RestController annotation plays a
3 min read
Spring MVC - Model Interface
The Spring Web model-view-controller (MVC) is an open-source framework used to build J2EE web applications. It is based on the Model-View-Controller design pattern and implements the basic features of a core spring framework - Dependency Injection. It is designed around a 'DispatcherServlet' that di
7 min read
Spring MVC â Implementing File Uploads and Downloads
Spring MVC is a widely used framework for developing robust web applications in Java. It simplifies handling HTTP requests and responses, including file uploads and downloads. File uploads enable users to send files to the server, while file downloads allow users to retrieve files from the server. T
6 min read
Spring - Multi Action Controller with Example
Spring is one of the most popular Java EE frameworks. It is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made
4 min read
JSP - File Downloading
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 i
3 min read
Spring - MVC Form Handling
Prerequisites: Spring MVC, Introduction to Spring Spring MVC is a Model-View-Controller framework, it enables the separation of modules into Model, View, and Controller and uniformly handles the application integration. In this article, we will create a student login form and see how Spring MVC hand
6 min read
How to Create Your First Model in Spring MVC?
Spring MVC is a powerful Web MVC framework for building web applications. It is designed around the Model-View-Controller (MVC) pattern, which separates the application into three main components:Model: Represents the data of the application. It can be a single object or a collection of objects.View
6 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
Spring MVC - Exception Handling
Prerequisites: Spring MVC When something goes wrong with your application, the server displays an exception page defining the type of exception, the server-generated exception page is not user-friendly. Spring MVC provides exception handling for your web application to make sure you are sending your
6 min read