Spring MVC sample application for downloading files
- 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:<%@ 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>Download Page</title> </head> <body> <center> <h2><a href="/download.do">Click here to download file</a></h2> </center> </body> </html>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
package net.codejava.spring; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping("/download.do") public class FileDownloadController { /** * Size of a byte buffer to read/write file */ private static final int BUFFER_SIZE = 4096; /** * Path of the file to be downloaded, relative to application's directory */ private String filePath = "/downloads/SpringProject.zip"; /** * Method for handling file download request from client */ @RequestMapping(method = RequestMethod.GET) public void doDownload(HttpServletRequest request, HttpServletResponse response) throws IOException { // get absolute path of the application ServletContext context = request.getServletContext(); String appPath = context.getRealPath(""); System.out.println("appPath = " + appPath); // construct the complete absolute path of the file String fullPath = appPath + filePath; File downloadFile = new File(fullPath); FileInputStream inputStream = new FileInputStream(downloadFile); // get MIME type of the file String mimeType = context.getMimeType(fullPath); if (mimeType == null) { // set to binary type if MIME mapping not found mimeType = "application/octet-stream"; } System.out.println("MIME type: " + mimeType); // set content attributes for the response response.setContentType(mimeType); response.setContentLength((int) downloadFile.length()); // set headers for the response String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); response.setHeader(headerKey, headerValue); // get output stream of the response OutputStream outStream = response.getOutputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; // write bytes read from the input stream into the output stream while ((bytesRead = inputStream.read(buffer)) != -1) { outStream.write(buffer, 0, bytesRead); } inputStream.close(); outStream.close(); } }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 WebContent\WEB-INF directory with the following content:<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 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="net.codejava.spring" /> <!-- your beans declaration goes here --> </beans>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:<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>FileDownloadSpringMVC</display-name> <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-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>SpringController</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> </web-app>
5. Required jar files
Add the following jar files into the WebContent\WEB-INF\lib 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.
Related File Download Tutorials:
- Java Servlet Download File Example
- Sample Struts application for downloading files
- Java FTP file download tutorial and example
- Use HttpURLConnection to download file from an HTTP URL
Other Spring Tutorials:
- Understand the core of Spring framework
- Understand Spring MVC
- Understand Spring AOP
- Spring Dependency Injection Example (Annotations)
- Spring MVC beginner tutorial with Spring Tool Suite IDE
- 14 Tips for Writing Spring MVC Controller
- Spring Web MVC Security Basic Example (XML Configuration)
- Understand Spring Data JPA with Simple Example
Comments
I want to show dialog box for downloading zip file from httpurlconnection. we have code in this site for downloading and save at given location . i want code to ask to save .. Thanks in advance if any1 help me on this
The link should be without slash:
Click here to download file
Sorry somehow the CMS always append the slash to the links.
its giving me 404 error but all files and path are correct..
its giving me 404 error but all files and path are correct..