Spring MVC File Download Example
Hello readers. Spring framework provides an out of box support for the file download functionality from the server to a local machine. In this tutorial, we will show you how to implement the file download functionality with the Spring Mvc framework. To handle the file download capability in a web application, we will use the HttpServletResponse
to directly write a file to the ServletOutputStream
.
Table Of Contents
1. Introduction
1.1 Spring Framework
- Spring is an open-source framework created to address the complexity of an enterprise application development
- One of the chief advantages of the Spring framework is its layered architecture, which allows developer to be selective about which of its components they can use while providing a cohesive framework for
J2EE
application development - Spring framework provides support and integration to various technologies for e.g.:
- Support for Transaction Management
- Support for interaction with the different databases
- Integration with the Object Relationship frameworks for e.g. Hibernate, iBatis etc
- Support for Dependency Injection which means all the required dependencies will be resolved with the help of containers
- Support for
REST
style web-services
1.1.1 Spring Mvc Framework
Model-View-Controller (Mvc) is a well-known design pattern for designing the GUI based applications. It mainly decouples the business logic from UI by separating the roles of Model, View, and Controller in an application. This pattern divides the application into three components to separate the internal representation of the information from the way it is being presented to the user. The three components are:
- Model (M): Model’s responsibility is to manage the application’s data, business logic, and the business rules. It is a
POJO
class which encapsulates the application data given by the controller - View (V): A view is an output representation of the information, such as displaying information or reports to the user either as a text-form or as charts. Views are usually the
JSP
templates written with Java Standard Tag Library (JSTL
) - Controller (C): Controller’s responsibility is to invoke the Models to perform the business logic and then update the view based on the model’s output. In spring framework, the controller part is played by the Dispatcher Servlet
1.2 Spring Framework’s Support for File Download
Spring Mvc framework provides several approaches for downloading a file in the Spring Mvc web-application. These are:
HttpServletRespone
: Developers can use theHttpServletResponse
object to directly write a file to theServletOutputStream
for the downloading purposeResponseEntity<InputStreamResource>
: Developers can return a file as anInputStreamResource
object which will be wrapped in aResponseEntity
from the Spring controller handler’s methodResponseEntity<ByteArrayResource>
: Developers can also return a file as aByteArrayResource
object wrapped in theResponseEntity
The following picture depicts the workflow of the sample application we are going to build in this tutorial.
Now, open up the Eclipse Ide and let’s start building the application!
2. Spring Mvc File Download Example
Below are the steps involved in developing this application.
2.1 Tools Used
We are using Eclipse Kepler SR2, JDK 8 and Maven. Having said that, we have tested the code against JDK 1.7 and it works well.
2.2 Project Structure
Firstly, let’s review the final project structure, in case you are confused about where you should create the corresponding files or folder later!
2.3 Project Creation
This section will demonstrate on how to create a Java-based Maven project with Eclipse. In Eclipse IDE, go to File -> New -> Maven Project
.
In the New Maven Project window, it will ask you to select project location. By default, ‘Use default workspace location’ will be selected. Just click on next button to proceed.
Select the Maven Web App Archetype from the list of options and click next.
It will ask you to ‘Enter the group and the artifact id for the project’. We will input the details as shown in the below image. The version number will be by default: 0.0.1-SNAPSHOT
.
Click on Finish and the creation of a maven project is completed. If you observe, it has downloaded the maven dependencies and a pom.xml
file will be created. It will have the following code:
pom.xml
<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>SpringMvcDownloadFile</groupId> <artifactId>SpringMvcDownloadFile</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> </project>
We can start adding the dependencies that developers want like Spring Mvc, Servlet Api, MySQL, and Log4j
etc. Let’s start building the application!
3. Application Building
Below are the steps involved in developing this application.
3.1 Database & Table Creation
The following MySQL script is used to create a database called filedownload
with table: exam_result
. Open the MySQL or the workbench terminal and execute the SQL
script:
CREATE DATABASE IF NOT EXISTS filedownload; USE filedownload; CREATE TABLE exam_result ( student_id INTEGER NOT NULL, student_name VARCHAR(30) NOT NULL, student_dob DATE NOT NULL, student_percentage double NOT NULL ); INSERT INTO exam_result (student_id, student_name, student_dob, student_percentage) VALUES (101, 'Harry Potter', '1993-02-01', 92); INSERT INTO exam_result (student_id, student_name, student_dob, student_percentage) VALUES (102, 'Java Code Geek', '1987-02-03', 62); INSERT INTO exam_result (student_id, student_name, student_dob, student_percentage) VALUES (103, 'Hermione Granger', '1985-02-01', 76); INSERT INTO exam_result (student_id, student_name, student_dob, student_percentage) VALUES (104, 'Lucifer Morningstar', '1965-02-01', 83); DESC exam_result; SELECT * FROM exam_result;
If everything goes well, the database and the table will be shown in the MySQL Workbench.
3.2 Maven Dependencies
In this example, we are using the most stable Spring web-mvc, MySQL, and Log4j
version in order to set-up the file download functionality. The updated file will have the following code:
pom.xml
<?xml version="1.0" encoding="UTF-8"?> <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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>SpringMvcDownloadFile</groupId> <artifactId>SpringMvcDownloadFile</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringMvcDownloadFile Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <!-- Spring Framework Dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.3.11.RELEASE</version> </dependency> <!-- Servlet API Dependency --> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>3.0-alpha-1</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> </dependency> <!-- MySQL Connector Java Dependency --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> <!-- Log4J Dependency --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> </dependencies> <build> <finalName>${project.artifactId}</finalName> </build> </project>
3.3 Java Class Creation
Let’s create the required Java files. Right-click on src/main/java
folder, New -> Package
.
A new pop window will open where we will enter the package name as: com.jcg.spring.mvc.file.download
.
Once the package is created, we will need to create the implementation class. Right-click on the newly created package, New -> Class
.
A new pop window will open and enter the file name as: FileDownloadController
. The spring controller class will be created inside the package: com.jcg.spring.mvc.file.download
.
3.3.1 Implementation of Controller Class
This is a typical spring controller class which is annotated by the Spring MVC annotation types. The methods downloadPdf()
or the downloadCsv()
will receive requests from the client. These two methods will read the file on the server and send it back to the client for downloading. Note that, unlike the traditional spring controller’s methods, these methods do not return a view-name as the application’s purpose is to send a file to the client. The method scope is completed as soon as the file is completely transferred to the client.
Let’s write a quick Java program in the spring controller class to handle the file download requests. Add the following code to it.
FileDownloadController.java
package com.jcg.spring.mvc.file.download; import java.io.File; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.servlet.ModelAndView; @Controller public class FileDownloadController { static ModelAndView modelViewObj; private static Logger logger = Logger.getLogger(FileDownloadController.class); @RequestMapping(value = {"/", "fileDownload"}, method = RequestMethod.GET) public ModelAndView showUploadFileForm(ModelMap model) { modelViewObj = new ModelAndView("fileDownload"); return modelViewObj; } @RequestMapping(value = "downloadFile/pdf", method = RequestMethod.GET) public void downloadPdf(HttpServletRequest req,HttpServletResponse resp) throws IOException { String pdfFilePath = "", pdfFileName = "irregular-verbs.pdf"; logger.info("Downloading A .PDF File From The Server ....!"); /**** Get The Absolute Path Of The File ****/ pdfFilePath = Util.getFilePath(req) + File.separator + pdfFileName; logger.info("Absolute Path Of The .PDF File Is?= " + pdfFilePath); File downloadFile = new File(pdfFilePath); if(downloadFile.exists()) { Util.downloadFileProperties(req, resp, pdfFilePath, downloadFile); } else { logger.info("Requested .PDF File Not Found At The Server ....!"); } } @RequestMapping(value = "downloadFile/csv", method = RequestMethod.GET) public void downloadCsv(HttpServletRequest req,HttpServletResponse resp) throws IOException { String csvFilePath = ""; logger.info("Downloading A .CSV File From The Server ....!"); /**** Get The Absolute Path Of The File ****/ csvFilePath = GenerateCsvData.writeDbDataToCsvFile(Util.getFilePath(req)); logger.info("Absolute Path Of The .CSV File Is?= " + csvFilePath); File downloadFile = new File(csvFilePath); if(downloadFile.exists()) { Util.downloadFileProperties(req, resp, csvFilePath, downloadFile); } else { logger.info("Requested .CSV File Not Found At The Server ....!"); } } }
3.4 Configuration Files
Let’s write all the configuration files involved in this application.
3.4.1 Spring Configuration File
To configure the spring framework, we need to implement a bean configuration file i.e. spring-servlet.xml
which provide an interface between the basic Java class and the outside world. Put this XML
file in the SpringMvcDownloadFile/src/main/webapp/WEB-INF
folder and add the following code to it:
spring-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:component-scan base-package="com.jcg.spring.mvc.file.download" /> <!-- Resolves Views Selected For Rendering by @Controllers to *.jsp Resources in the /WEB-INF/ Folder --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <!-- File Download Exception Resolver i.e. In Case Of Exception The Controller Will Navigate To 'error.jsp' & Will Display The Exception Message --> <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> <property name="exceptionMappings"> <props> <prop key="java.lang.Exception">error</prop> </props> </property> </bean> </beans>
3.4.2 Web Deployment Descriptor
The web.xml
file declares one servlet (i.e. Dispatcher Servlet) to receive all kind of the requests and specifies the default page when accessing the application. Dispatcher servlet here acts as a front controller. Add the following code to it:
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>Spring Mvc File Download Example</display-name> <!-- Spring Configuration - Processes Application Requests --> <servlet> <servlet-name>SpringController</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringController</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
3.5 Creating JSP Views
Spring Mvc supports many types of views for different presentation technologies. These include – JSP
, HTML
, XML
etc. So let us write a simple view in SpringMvcDownloadFile /src/main/webapp/WEB-INF/views
folder. This page simply shows the download file links which are handled by the methods in the spring controller class (i.e. FileDownloadController.java
). Add the following code to it:
fileDownload.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!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=ISO-8859-1"> <title>Spring Mvc File Download Example</title> <style type="text/css"> .linkCSS { cursor: pointer; text-decoration: none; } .padding { padding: 13px 0px 20px 145px; } </style> </head> <body> <center><h2>Spring Mvc File Download Example</h2></center> <div id="pdfFile" class="padding"> <a id="downloadPdfFileLink" target="_self" class="linkCSS" href="${pageContext.request.contextPath}/downloadFile/pdf">Download Pdf File</a> </div> <div id="csvField" class="padding"> <a id="downloadCsvFileLink" target="_self" class="linkCSS" href="${pageContext.request.contextPath}/downloadFile/csv">Download Csv File</a> </div> </body> </html>
4. Run the Application
As we are ready for all the changes, let us compile the project and deploy the application on the Tomcat7 server. To deploy the application on Tomat7, right-click on the project and navigate to Run as -> Run on Server
.
Tomcat will deploy the application in its web-apps folder and shall start its execution to deploy the project so that we can go ahead and test it in the browser.
5. Project Demo
Open your favorite browser and hit the following URL. The output page will be displayed.
http://localhost:8085/SpringMvcDownloadFile/
Server name (localhost) and port (8085) may vary as per your tomcat configuration. Developers can debug the example and see what happens after every step. Enjoy!
Click on the download link and the browser will ask to download the particular file.
That’s all for this post. Happy Learning!!
6. Conclusion
In this section, developers learned how to create a sample Spring Mvc application that allows the file download functionality. Developers can download the sample application as an Eclipse project in the Downloads section, and remember to update the database connection settings.
7. Download the Eclipse Project
This was an example of File Download with Spring Mvc.
You can download the full source code of this example here: SpringMvcDownloadFile
It could be better if you put
Util
class that has downloading functionality, it was the core of the article. Thank for your article.Tahta, hope you are doing good. The relevant files are already present in the code. Please download the project and let us know in case any further information is required from JCG end.
Hi iam always getting catalina life cycle exception.