Download file in mvc controller

Download file in mvc controller

download file in mvc controller

fileBytes, www.cronistalascolonias.com.ar, filePath);. byte[] fileBytes = GetFile(fullName);. Step 2: Create a mvc controller,Right click onthe "Controllers" folderand add "​Home" www.cronistalascolonias.com.ar and paste the following www.cronistalascolonias.com.ar the. download file in mvc controller

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
In 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 www.cronistalascolonias.com.ar 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 www.cronistalascolonias.com.ar file which resides in the downloads directory which is relative to the application’s directory. 

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:
    1. commons-loggingjar
    2. www.cronistalascolonias.com.ar
    3. www.cronistalascolonias.com.ar
    4. www.cronistalascolonias.com.ar
    5. www.cronistalascolonias.com.ar
    6. www.cronistalascolonias.com.ar
    7. www.cronistalascolonias.com.ar
The Commons Logging jar files can be downloaded from Apache Commons Logging, other jar files come from Spring framework RELEASE download.


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: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.

Related File Download Tutorials: 

 

Other Spring Tutorials:


About the Author:

Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Источник: www.cronistalascolonias.com.ar

Download file in mvc controller - authoritative answer

Download file in mvc controller

1 thoughts to “Download file in mvc controller”

Leave a Reply

Your email address will not be published. Required fields are marked *