Spring4 MVC文件下载实例

这篇文章将向您展示如何使用Spring MVC4执行文件下载,我们将看到应用程序从文件系统内部以及外部文件下载文件。

本教程的主要亮点:

下载文件是相当简单的,涉及以下步骤。

  • 创建一个InputStream到文件用于下载。
  • 查找MIME类型下载文件的内容。
    –可以是application/pdf, text/html,application/xml,image/png等等。
  • 将内容类型与上述发现的MIME类型响应(HttpServletResponse)。
    response.setContentType(mimeType);
  • 针对以上找到MIME类型设置内容长度。
    response.setContentLength(file.getLength());//length in bytes
  • 为响应设置内容处理标头。
    response.setHeader(“Content-Disposition”, “attachment; filename=” + fileName); //随着“附件”文件将下载。可能会显示一个“另存为”基于浏览器的设置对话框。

    response.setHeader(“Content-Disposition”, “inline; filename=” + fileName);//通过“内联”浏览器将尝试显示内容到浏览器中(图片,PDF,文本,...)。对于其他内容类型,文件将直接下载。
  • 从InputStream中复制字节响应到 OutputStream。
  • 一旦复制完成后,关闭输入输出流。

完整实施例在下面讨论。



使用到以下技术:

  • Spring 4.2.0.RELEASE
  • Bootstrap v3.3.2
  • Maven 3
  • JDK 1.7
  • Tomcat 8.0.21
  • Eclipse JUNO Service Release 2

现在让我们开始

项目结构

在pom.xml中声明依赖关系

<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.yiibai.springmvc</groupId>
  <artifactId>Spring4MVCFileDownloadExample</artifactId>
  <packaging>war</packaging>
  <version>1.0.0</version>
  <name>Spring4MVCFileDownloadExample Maven Webapp</name>

     <properties>
		<springframework.version>4.2.0.RELEASE</springframework.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${springframework.version}</version>
		</dependency>

		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
	</dependencies>

	<build>
		<pluginManagement>
			<plugins>
				<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-war-plugin</artifactId>
					<version>2.4</version>
					<configuration>
						<warSourceDirectory>src/main/webapp</warSourceDirectory>
						<warName>Spring4MVCFileDownloadExample</warName>
						<failOnMissingWebXml>false</failOnMissingWebXml>
					</configuration>
				</plugin>
			</plugins>
		</pluginManagement>

		<finalName>Spring4MVCFileDownloadExample</finalName>
	</build>
</project>
 

创建控制器

package com.yiibai.springmvc.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLConnection;
import java.nio.charset.Charset;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class FileDownloadController {

	private static final String INTERNAL_FILE="irregular-verbs-list.pdf";
	private static final String EXTERNAL_FILE_PATH="C:/mytemp/SpringMVCHibernateManyToManyCRUDExample.zip";

	@RequestMapping(value={"/","/welcome"}, method = RequestMethod.GET)
	public String getHomePage(ModelMap model) {
		return "welcome";
	}

	/*
	 * Download a file from
	 *   - inside project, located in resources folder.
	 *   - outside project, located in File system somewhere.
	 */
	@RequestMapping(value="/download/{type}", method = RequestMethod.GET)
	public void downloadFile(HttpServletResponse response, @PathVariable("type") String type) throws IOException {

		File file = null;

		if(type.equalsIgnoreCase("internal")){
			ClassLoader classloader = Thread.currentThread().getContextClassLoader();
			file = new File(classloader.getResource(INTERNAL_FILE).getFile());
		}else{
			file = new File(EXTERNAL_FILE_PATH);
		}

		if(!file.exists()){
			String errorMessage = "Sorry. The file you are looking for does not exist";
			System.out.println(errorMessage);
			OutputStream outputStream = response.getOutputStream();
			outputStream.write(errorMessage.getBytes(Charset.forName("UTF-8")));
			outputStream.close();
			return;
		}

		String mimeType= URLConnection.guessContentTypeFromName(file.getName());
		if(mimeType==null){
			System.out.println("mimetype is not detectable, will take default");
			mimeType = "application/octet-stream";
		}

		System.out.println("mimetype : "+mimeType);

        response.setContentType(mimeType);

        /* "Content-Disposition : inline" will show viewable types [like images/text/pdf/anything viewable by browser] right on browser
            while others(zip e.g) will be directly downloaded [may provide save as popup, based on your browser setting.]*/
        response.setHeader("Content-Disposition", String.format("inline; filename=\"" + file.getName() +"\""));

        /* "Content-Disposition : attachment" will be directly download, may provide save as popup, based on your browser setting*/
        //response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));

        response.setContentLength((int)file.length());

		InputStream inputStream = new BufferedInputStream(new FileInputStream(file));

        //Copy bytes from source to destination(outputstream in this example), closes both streams.
        FileCopyUtils.copy(inputStream, response.getOutputStream());
	}

}

该控制器包括两个文件。一个文件是内部应用(内部资源),和其他文件位于外部的应用程序的文件系统。您的项目一定要改变外部文件的路径。仅用于演示的目的,我们已在路径一个额外的路径变量(内部/外部)。我们正在使用Spring FileCopyUtils工具类流从源复制到目的地。

配置

package com.yiibai.springmvc.configuration;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.yiibai.springmvc")
public class HelloWorldConfiguration extends WebMvcConfigurerAdapter{

	@Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");
        registry.viewResolver(viewResolver);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/static/");
    }

}

初始化

package com.yiibai.springmvc.configuration;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class HelloWorldInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] { HelloWorldConfiguration.class };
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return null;
    }

    @Override
    protected String[] getServletMappings() {
        return new String[] { "/" };
    }

 }

添加视图

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
	<title>Spring 4 MVC File Download Example</title>
	<link href="<c:url value=‘/static/css/bootstrap.css‘ />"  rel="stylesheet"></link>
	<link href="<c:url value=‘/static/css/app.css‘ />" rel="stylesheet"></link>
</head>
<body>
	<div class="form-container">
		<h1>Welcome to FileDownloader Example</h1>

		Click on below links to see FileDownload in action.<br/><br/>

		<a href="<c:url value=‘/download/internal‘ />">Download This File (located inside project)</a>
		<br/>
		<a href="<c:url value=‘/download/external‘ />">Download This File (located outside project, on file system)</a>

	</div>
</body>
</html>

构建,部署和运行应用程序

现在构建war(在前面的Eclipse教程)或通过Maven的命令行( mvn clean install)。部署 war 到Servlet3.0容器。或:

打开浏览器,浏览 http://localhost:8080/Spring4MVCFileDownloadExample

点击第二个链接。外部文件应被下载。

点击第一个链接。内部文件[这是一个PDF]应该显示在浏览器中,这是由于 Content-Disposition: inline. 通过内联,如果内容可以通过浏览器显示,它会显示它在浏览器中。

现在从内联更改内容处置备注。构建并部署。点击第一个链接。这个时候您应该看到 PDF文件被下载。

就这样,完成!

下载代码:http://pan.baidu.com/s/1c1lmeL6

时间: 2024-08-05 07:31:40

Spring4 MVC文件下载实例的相关文章

Spring4 MVC HelloWord实例

本教程是基于以下工具写的: MyEclipse 10 Spring 4.0.3.RELEASE 2- 预览应用程序执行流程 Spring MVC DispatcherServlet 读取 xml 配置文件的原则: {servlet-name} ==> /WEB-INF/{servlet-name}-servlet.xml 如果你不想用 SpringMVC 的使用原则,可以重新配置 SpringMVC  DispatcherServlet 在 web.xml 文件中: <servlet>

Spring4+MVC+Hibernate4全注解环境搭建(一)

声明: 以下任何观点.理解,都有可能是错的,那仅代表作者在某一时刻结合自己的学习经历和思考得出的观点,很明显,这样的前提下很多都可能是错的.请各位在看到任何可疑观点时,都不要轻信,如果你们在喷我的时候能把理由一并说出来,那我就非常感激了.像什么“你懂的”,“当然是!不然还能是什么.”那样的话恐怕既说服不了我,也说服不了别人. 目前为止我对这几个框架认识: 我的理解不一定对,但是我还是在此首先明确一下我为什么选择的是Spring4+MVC+Hibernate4. Spring就是用来提供一个IoC

Spring4 MVC ContentNegotiatingViewResolver多种输出格式实

前段时间在一个项目里面发现,针对Excel的处理没有一个公用的视图,来个下载的需求就要自己去写一堆POI的东西,终于有一天给我也来了几个,还是按照以前的方式来写,写多了真心想吐,后面想想还是有必要整个解析Excel的视图了.花了一天时间,总结出来共有三种方式可以处理Excel视图. 由于spring已经提供了excel的抽象视图,所以我们直接继承过来就可以了.由于POI对excel2007和2003处理方式有些不同,所以spring4.2以上版本提供了两个excel抽象视图AbstractXls

Spring4 MVC Hibernate4集成

Spring4 MVC Hibernate4集成   Spring4 MVC Hibernate4集成 一.    本文所用环境 Spring4.0.3.RELEASE Hibernate4.3.5.Final Mysql 二.    工程目录 三.    Maven添加依赖 用Maven创建项目,pom.xml如下:   四.    新建数据库表 数据库采用Mysql,新建users表,我们演示操作此表 CREATE TABLE `users` ( `id` int(11) NOT NULL

Spring MVC 文件下载时候 发现IE不支持

@RequestMapping("download") public ResponseEntity<byte[]> download(Long fileKey) throws IOException { HttpHeaders headers = new HttpHeaders(); String fileName=new String(massMessage.getFileName().getBytes("UTF-8"),"iso-8859-

Spring MVC 入门实例

概述: springmvc 框架围绕DispatcherServlet这个核心展开,DispatcherServlet是Spring MVC的总控制,它负责截获请求并将其分派给相应的处理器处理.SpringMVC框架包括注解驱动控制器.请求及响应的信息处理.视图解析.本地化解析.上传文件解析.异常处理以及表单标签绑定等内容. SpringMVC是主要基于MODEL2实现的技术框架,Model2是经典的MVC(model.view.control)模型在web应用中的变体,这个改变主要源于HTTP

ASP.net MVC 文件下载的几种方法

ASP.net MVC 文件下载的几种方法(欢迎讨论) 在ASP.net MVC 中有几种下载文件的方法前提:要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的.第一种:最简单的超链接方法,<a>标签的href直接指向目标文件地址,这样容易暴露地址造成盗链,这里就不说了第二种:后台下载在后台下载中又可以细分为几种下载方式首先,在前台,我们需要一个<a>标签 <a href="~/Home/download&q

Spring MVC框架实例

Spring  MVC 背景介绍 Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring 可插入的 MVC 架构,可以选择是使用内置的 Spring Web 框架还是 Struts 这样的 Web 框架.通过策略接口,Spring 框架是高度可配置的,而且包含多种视图技术,例如 JavaServer Pages(JSP)技术.Velocity.Tiles.iText 和 POI.Spring MVC 框架并不知道使用的视图,所以不会强迫您只使用 JSP 技术.S

spring4 spring4 mvc视频学习资源

最近几天,想重新回顾和加强下spring技术,在网上搜到以下资源: 百度云盘: spring4 http://pan.baidu.com/s/1kTn7rSz#path=%252F%255BIT%25E6%2595%2599%25E7%25A8%258B%25E7%25BD%2591-www.itjiaocheng.com%255D%25E5%25B0%259A%25E7%25A1%2585%25E8%25B0%25B7Spring%25E8%25A7%2586%25E9%25A2%2591%2