
Think: Download file in mvc controller
| WHY DOWNLOAD DRIVERS FOR NVIDIA REDDIT | |
| INGLORIOUS BASTARDS MP4 DOWNLOAD | |
| FSX STEAM PC GAME FREE DOWNLOAD | |
| SPOTIFY HOW TO DOWNLOAD LOCAL FILES ON IPHONE |
Spring MVC sample application for downloading files
- Details
- Written by Nam Ha Minh
- Last Updated on 25 June   |  Print Email
1. Code of download page
Create www.cronistalascolonias.com.ar file under WebContent directory with the following HTML code:<%@ page language="java" contentType="text/html; charset=ISO" pageEncoding="ISO"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML Transitional//EN" "www.cronistalascolonias.com.ar"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO"> <title>Download Page</title> </head> <body> <center> <h2><a href="/www.cronistalascolonias.com.ar">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: www.cronistalascolonias.com.ar. We’ll configure Spring controller class to handle this URL.
2. Code of Spring controller class
Create www.cronistalascolonias.com.ar file under the source package www.cronistalascolonias.com.ar with the following code:package www.cronistalascolonias.com.ar; import www.cronistalascolonias.com.ar; import www.cronistalascolonias.com.arputStream; import www.cronistalascolonias.com.arption; import www.cronistalascolonias.com.arStream; import www.cronistalascolonias.com.artContext; import www.cronistalascolonias.com.arrvletRequest; import www.cronistalascolonias.com.arrvletResponse; import www.cronistalascolonias.com.arller; import www.cronistalascolonias.com.artMapping; import www.cronistalascolonias.com.artMethod; @Controller @RequestMapping("/www.cronistalascolonias.com.ar") public class FileDownloadController { /** * Size of a byte buffer to read/write file */ private static final int BUFFER_SIZE = ; /** * Path of the file to be downloaded, relative to application's directory */ private String filePath = "/downloads/www.cronistalascolonias.com.ar"; /** * Method for handling file download request from client */ @RequestMapping(method = www.cronistalascolonias.com.ar) public void doDownload(HttpServletRequest request, HttpServletResponse response) throws IOException { // get absolute path of the application ServletContext context = www.cronistalascolonias.com.arvletContext(); String appPath = www.cronistalascolonias.com.arlPath(""); www.cronistalascolonias.com.arn("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 = www.cronistalascolonias.com.areType(fullPath); if (mimeType == null) { // set to binary type if MIME mapping not found mimeType = "application/octet-stream"; } www.cronistalascolonias.com.arn("MIME type: " + mimeType); // set content attributes for the response www.cronistalascolonias.com.artentType(mimeType); www.cronistalascolonias.com.artentLength((int) www.cronistalascolonias.com.ar()); // set headers for the response String headerKey = "Content-Disposition"; String headerValue = www.cronistalascolonias.com.ar("attachment; filename=\"%s\"", www.cronistalascolonias.com.are()); www.cronistalascolonias.com.arder(headerKey, headerValue); // get output stream of the response OutputStream outStream = www.cronistalascolonias.com.arputStream(); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead = -1; // write bytes read from the input stream into the output stream while ((bytesRead = www.cronistalascolonias.com.ar(buffer)) != -1) { www.cronistalascolonias.com.ar(buffer, 0, bytesRead); } www.cronistalascolonias.com.ar(); www.cronistalascolonias.com.ar(); } }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 www.cronistalascolonias.com.ar file under WebContent\WEB-INF directory with the following content:<?xml version="" encoding="UTF-8"?> <beans xmlns="www.cronistalascolonias.com.ar" xmlns:xsi="www.cronistalascolonias.com.ar" xmlns:context="www.cronistalascolonias.com.ar" xsi:schemaLocation="www.cronistalascolonias.com.ar www.cronistalascolonias.com.ar www.cronistalascolonias.com.ar www.cronistalascolonias.com.ar"> <context:component-scan base-package="www.cronistalascolonias.com.ar" /> <!-- your beans declaration goes here --> </beans>This is a deadly simple Spring configuration file which tells the framework to scan the package www.cronistalascolonias.com.ar 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 www.cronistalascolonias.com.ar
The Spring dispatcher servlet is configured to handle requests in the www.cronistalascolonias.com.ar file as follows:<?xml version="" encoding="UTF-8"?> <web-app xmlns:xsi="www.cronistalascolonias.com.ar" xmlns="www.cronistalascolonias.com.ar" xmlns:web="www.cronistalascolonias.com.ar" xsi:schemaLocation="www.cronistalascolonias.com.ar www.cronistalascolonias.com.ar" id="WebApp_ID" version=""> <display-name>FileDownloadSpringMVC</display-name> <servlet> <servlet-name>SpringController</servlet-name> <servlet-class>www.cronistalascolonias.com.archerServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/www.cronistalascolonias.com.ar</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-loggingjar
- www.cronistalascolonias.com.ar
- www.cronistalascolonias.com.ar
- www.cronistalascolonias.com.ar
- www.cronistalascolonias.com.ar
- www.cronistalascolonias.com.ar
- www.cronistalascolonias.com.ar
6. Testing the application
Deploy the application on localhost Tomcat server, type the following URL into browser’s address bar:http://localhost/FileDownloadSpringMVC/
The download page is displayed:- 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:
-