[转] Spring MVC sample application for downloading files
2014-06-19 13:59
323 查看
http://www.codejava.net/frameworks/spring/spring-mvc-sample-application-for-downloading-files
n this article, we are going to show you how to implement file download functionality in a Spring MVC application. The solution is similar to the one described in the article: Send files from servlet to client for downloading, but is implemented in a Spring MVC application.
The following picture depicts workflow of the sample application we are going to build:
Project structure (Eclipse project):
The file to be downloaded in this application is SpringProject.zip file which resides in the downloads directory which is relative to the application’s directory.
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.
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.
Recommended Book: Getting started with Spring Framework
Create spring-mvc.xml file under WebContent\WEB-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.
Related Course: The Java Spring Tutorial
The Spring dispatcher servlet is configured to handle requests in the web.xml file as follows:
Recommended Book: Spring in Action
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
The Commons Logging jar files can be downloaded from Apache Commons Logging, other jar files come from Spring framework 3.2.1 RELEASE download.
Related Course: The Java Spring Tutorial
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.
Recommended Book: Spring in Practice
n this article, we are going to show you how to implement file download functionality in a Spring MVC application. The solution is similar to the one described in the article: Send files from servlet to client for downloading, but is implemented in a Spring MVC application.
The following picture depicts workflow of the sample application we are going to build:
Project structure (Eclipse project):
The file to be downloaded in this application is SpringProject.zip file which resides in the downloads directory which is relative to the application’s directory.
1. Code of download page
Create index.jsp file under WebContent directory with the following HTML code:
2. Code of Spring controller class
Create FileDownloadController.java file under the source package net.codejava.spring with the following code:Recommended Book: Getting started with Spring Framework
3. Code of Spring configuration file
Create spring-mvc.xml file under WebContent\WEB-INF directory with the following content:Related Course: The Java Spring Tutorial
4. Code of web.xml
The Spring dispatcher servlet is configured to handle requests in the web.xml file as follows:
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
The Commons Logging jar files can be downloaded from Apache Commons Logging, other jar files come from Spring framework 3.2.1 RELEASE download.
Related Course: The Java Spring Tutorial
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.
Recommended Book: Spring in Practice
相关文章推荐
- Spring MVC sample application for downloading files
- Idea - Spring Boot - MVC 出现This application has no explicit mapping for /error, so you are seeing th
- Preparing for the Sample Application
- Sample Application using JSF, Spring 2.0, and Java Persistence APIs
- Unit Tests for ASP.NET MVC application that uses resources in code behind.
- How To Create Patch Files for the MSDE 2000 Sample.msi File
- sample-spring-memecached+mybatis+spring mvc例子
- Developing a Spring Framework MVC application step-by-step
- 初窥spring3 MVC for annotation
- spring mvc page not found no mapping found for http request with uri
- Spring起步例子 “Developing a Spring Framework MVC application step-by-step” 剖析(二)
- Moving property files outside jar in spring standalone application
- Sample Application using JSF, Spring 2.0, and Java Persistence APIs
- ASP.NET MVC Uploading and Downloading Files
- ASP.NET MVC Uploading and Downloading Files
- Hibernate + JPA + Spring MVC integrated Web Application - 4 easy steps
- Spring起步例子 “Developing a Spring Framework MVC application step-by-step” 剖析 (一)
- A class which is good for downloading files
- Developing a Spring Framework MVC application step-by-step
- Spring3MVC:Create Hello World application in Spring 3.0 MVC