Spring MVC download files(03)
FRAMEWORK/SPRING 2014. 12. 2. 14:51SpringMVCDownloadFiles.7z
Spring MVC download files
In this tutorial you will see how to create Spring MVC application to download files from client interface:
- pom.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javahonk</groupId> <artifactId>SpringMVCDownloadFiles</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>SpringMVCDownloadFiles Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <SpringVersion>4.0.6.RELEASE</SpringVersion> </properties> <dependencies> <!-- Spring dependencies --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${SpringVersion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${SpringVersion}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${SpringVersion}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> </dependencies> <build> <finalName>SpringMVCDownloadFiles</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.0</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> </plugins> </build> </project> |
- web.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.web</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher-servlet.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <welcome-file-list> <welcome-file>helloWorld.web</welcome-file> </welcome-file-list> </web-app> |
- helloWorld.jsp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Spring MVC download files</title> </head> <body> <h2>Spring MVC download files: ${message}</h2> <b>CustomJar.jar</b> <a href="downloadFiles.web">Click and download file here</a> </body> </html> |
- dispatcher-servlet.xml:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.javahonk.controller" /> <mvc:annotation-driven /> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix"> <value>/WEB-INF/jsp/</value> </property> <property name="suffix"> <value>.jsp</value> </property> </bean> </beans> |
- SpringMVCController.java:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | package com.javahonk.controller; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class SpringMVCController { @RequestMapping(value = "/helloWorld.web", method = RequestMethod.GET) public String printWelcome(ModelMap model) { model.addAttribute("message", "Sample code!"); return "helloWorld"; } @RequestMapping(value = "/downloadFiles.web", method = RequestMethod.GET) public @ResponseBody void downloadFiles(HttpServletRequest request, HttpServletResponse response) { ServletContext context = request.getServletContext(); File downloadFile = new File("C:/JavaHonk/CustomJar.jar"); FileInputStream inputStream = null; OutputStream outStream = null; try { inputStream = new FileInputStream(downloadFile); response.setContentLength((int) downloadFile.length()); response.setContentType(context.getMimeType("C:/JavaHonk/CustomJar.jar")); // response header String headerKey = "Content-Disposition"; String headerValue = String.format("attachment; filename=\"%s\"",downloadFile.getName()); response.setHeader(headerKey, headerValue); // Write response outStream = response.getOutputStream(); IOUtils.copy(inputStream, outStream); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != inputStream) inputStream.close(); if (null != inputStream) outStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
출처 -http://javahonk.com/spring-mvc-download-files/
'FRAMEWORK > SPRING' 카테고리의 다른 글
파일 업로드 (0) | 2015.01.07 |
---|---|
Spring MVC download files(04) (0) | 2014.12.02 |
Spring MVC download files(02) (0) | 2014.12.02 |
Spring MVC download files(01) (0) | 2014.12.02 |
Spring MVC with Excel View Example (Apache POI and JExcelApi) (0) | 2014.11.19 |