[ת] Spring MVC sample application for downloading files
- 作者: 五速梦信息网
- 时间: 2026年04月04日 13:58
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 pageCreate 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 classCreate 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:
3. Code of Spring configuration fileCreate 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:
4. Code of web.xmlThe Spring dispatcher servlet is configured to handle requests in the web.xml file as follows:
Recommended Book:
5. Required jar filesAdd 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:
6. Testing the applicationDeploy the application on localhost Tomcat server, type the following URL into browser’s address bar:
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:
相关文章
-
[转] 关于 Ceph PG
[转] 关于 Ceph PG
- 互联网
- 2026年04月04日
-
[转] 前后端分离开发模式的 mock 平台预研
[转] 前后端分离开发模式的 mock 平台预研
- 互联网
- 2026年04月04日
-
[转][cesium]1.添加本地服务器
[转][cesium]1.添加本地服务器
- 互联网
- 2026年04月04日
-
[置顶] 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!
[置顶] 纯手工打造漂亮的垂直时间轴,使用最简单的HTML+CSS+JQUERY完成100个版本更新记录的华丽转身!
- 互联网
- 2026年04月04日
-
[置顶] Jsp中的table多表头导出excel文件
[置顶] Jsp中的table多表头导出excel文件
- 互联网
- 2026年04月04日
-
[置顶] C语言中各种数据类型的长度 sizeof char, short, int, long, long long
[置顶] C语言中各种数据类型的长度 sizeof char, short, int, long, long long
- 互联网
- 2026年04月04日






