您的位置:首页 > 移动开发

[转] 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.

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.

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:

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




4. Code of web.xml

The Spring dispatcher servlet is configured to handle requests in the web.xml file as follows:

Recommended Book: Spring in Action


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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: