- Mvc Download File Example
- Spring Mvc Download Excel File Icon
- Spring Mvc Download Excel File
- Spring Mvc File Download Example
- Spring Mvc Download Excel File Example
Export as excel is one of the most wanted feature in an enterprise application. In this tutorial let us learn about export as excel feature using Spring MVC framework. If you are a beginner, go through the Spring MVC tutorial before taking this. We will be using Spring 3 annotation based approach. In Spring MVC application, to download a resource such as a file to the browser, you need to do the following in your controller. Use the void return type for your request-handling method and add HttpServletResponse as an argument to the method. Set the response’s content type to the file’s. Aug 17, 2010 Spring MVC comes with AbstractExcelView class to export data to Excel file via Apache POI library. In this tutorial, it show the use of AbstractExcelView class in Spring MVC application to export data to Excel file for download.
- Details
- Written by Nam Ha Minh
- Last Updated on 25 June 2019 | Print Email
1. Code of download page
Create index.jsp file under WebContent directory with the following HTML code:This page simply shows a link “Click here to download file” with URL points to the relative path: download.do. We’ll configure Spring controller class to handle this URL.
2. Code of Spring controller class
Create FileDownloadController.java file under the source package net.codejava.spring with the following code:This is a typical Spring controller class which is annotated by Spring MVC annotation types. The method doDownload() will receive requests from the client, read the file on server and send it to the client for downloading. Note that, unlike traditional Spring controller’s methods, the method doDownload()does not return a view name, because our purpose is to send a file to the client. The method exits as soon as the file is completely transferred to the client.
3. Code of Spring configuration file
Create spring-mvc.xml file under WebContentWEB-INF directory with the following content:This is a deadly simple Spring configuration file which tells the framework to scan the package net.codejava.spring for annotated types (element <context:component-scan />). Of course your application will have some bean definitions, but for the purpose of this application, such configuration is enough to work.
4. Code of web.xml
The Spring dispatcher servlet is configured to handle requests in the web.xml file as follows:Mvc Download File Example
5. Required jar files
Add the following jar files into the WebContentWEB-INFlib directory:- commons-logging-1.1.1.jar
- spring-beans-3.2.1.RELEASE.jar
- spring-context-3.2.1.RELEASE.jar
- spring-core-3.2.1.RELEASE.jar
- spring-expression-3.2.1.RELEASE.jar
- spring-web-3.2.1.RELEASE.jar
- spring-webmvc-3.2.1.RELEASE.jar
6. Testing the application
Deploy the application on localhost Tomcat server, type the following URL into browser’s address bar:http://localhost:8080/FileDownloadSpringMVC/
The download page is displayed:Click on the link, the browser will ask to download the file:You can download Eclipse project for this application as well as deployable WAR file in the attachment section below.NOTES:One may ask why not just putting a file somewhere on the server and give the users a link to download it? Of course that will work, however that is a static way. By handling the file to be downloaded programmatically, we can obtain the following benefits:- Delivering the files dynamically, based on user’s requests.
- Controlling access to the files: who can download and when the download is available.
- Hiding the actual location of files on the server.
Spring Mvc Download Excel File Icon
Spring Mvc Download Excel File
Related File Download Tutorials:
Other Spring Tutorials:
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook.[Deployable WAR file] | 3341 kB |
[Eclipse project] | 3344 kB |
I'am working on an excel export functionality in one of my webapps. I set up a little test case and got the download working, but the xlsx file is corrupted and don't know what else I could try. If I write the excel to file it opens without problem, so the error must occure when downloading.
Spring Mvc File Download Example
The setup:
spring-mvc 3.2.7poi 3.10.1Tomcat 8.0
Controller method:
Abstract Custom View:
ExcelBuilder:
Response Header:
It confuses me that the charset will be set, when this is binary data. Could that be the problem?
2 Answers
Don't return ModelAndView but just write the excel file to the response's outputStream
Check all the streams flushed/closed
StanislavLStanislavLI would suggest to use existing solution rather than trying to deal with the response stream by yourself.
I would use AbstractExcelView instead of your AbstractPOIExcelView
to do the job. Check this tutorial using AbstractExcelView for inspiration.
For Spring 4.2 or newer use AbstractXlsView (or AbstractXlsxView) because the original AbstractExcelView
is deprecated.