Spring MVC Download File Example

This tutorial shows you the Spring MVC Download File Example by using HttpServletResponse, HttpEntity and Spring FileSystemResource. So you can either download a file as an attachment or directly view it inside the browser.

Table of contents:
1. Maven Dependencies
2. Project Structure
3. Three ways to download file
4. Spring MVC Download File Controller
5. Exception handling
6. Download View
6. Deploy Spring MVC Download File Example

Other interesting posts you may like

Maven Dependencies

Project Structure
Spring MVC Download File Example project

Three ways to download file
Download File using HttpServletResponse
Firstly, we have to set the header properties like Content-Type, Content-Disposition and Content-Length. The Content-Disposition property instructs the browser which filename is used and whether the the file is downloaded as attachment or displayed in the browser. Secondly, We use the HttpServletResponse to write a file directly to the ServletOutputStream.
The code snippet show you the file is downloaded as an attachment.

Download File using HttpEntity
The HttpEntity is used to download file also. And we set the headers like Content-Type, Content-Disposition and Content-Length. But we instruct the browser to display the file by using inline here (if the file type is supported by browser).

Download File using Resource
We can use the FileSystemResource of Spring MVC that helps to manage the streams. Beside, Spring also supports many implementations of the Resource class, ClassPathResource and UrlResource for example.

Spring MVC Download File Controller
Here, we implement a Spring MVC controller (MyDownloadController for example) to download a file using three ways above.

Exception handling
The exception processing is necessary while working with a file. The exception can be thrown such as FileNotFoundException, IOException and so so. In this tutorial, we will handle two exceptions by annotating the MyExceptionHandler class with @ControllerAdvice. And the @ExceptionHandler annotation in conjunction with the specified exception are annotated with the appropriate methods.

Download View
Now, we create the view to demonstrate the Spring MVC Download File Example.

Deploy Spring MVC Download File Example
Building project with maven then deploy file war on application server or servlet container (Tomcat 8 for example). Access the address URL http://localhost:8080/download-file-example/ and the screen will display such as the picture

Spring MVC Download File Example

Download the file as an attachment by clicking the link “Click me to download file using servlet response” and the screen shot should be
Spring MVC Download File Example attachment

Download the file as an inline by clicking the link “Click me to download file using http entity.” or “Click me to download file using file system resource.” and the screen shot should be

Spring MVC Download File Example inline

That’s it on the tutorial Spring MVC Download File Example. If you want to know how to upload a file using Spring MVC, please refer to the link Spring MVC File Upload Validation Example

Download complete source code, please click link below

Spring-MVC-Download-File-Example.zip (104 downloads)

Source code on Github https://github.com/javabycode/spring-mvc-download-file-example

Leave a Comment

*

Please share it if you found this useful
Hide Buttons