Spring MVC 文件上传与下载

来源:互联网 发布:三菱plc步进指令编程 编辑:程序博客网 时间:2024/05/16 18:16

Spring MVC 为文件上传提供了直接的支持,而文件下载又可以通过 ResponseEntity 对象简单实现。

这里给出一个文件上传与下载的简单例子。

环境

  • JDK1.7
  • Tomcat 7
  • Spring MVC 4.2.0 RELEASE

注:Spring MVC 相关 jar 包我已经上传到 这里 ,包括 Spring 的全部 jar、commons 相关 jar 和 Hibernate-Validator 的相关 jar。commons-io.jar 和 commons-fileupload.jar 是文件上传与下载所需的 jar 包。

项目结构

这里写图片描述

1 新建项目工程

(1)首先在 Eclipse 里新建一个动态 Web 工程(Dynamic Web Project),命名为 FileTest ,并自动生成 web.xml。
(2)将前面下载的 jar 包拷贝到工程的 WebContent/WEB-INF/lib/ 目录下。

2 文件上传

2.1 配置 web.xml 文件

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    id="WebApp_ID" version="3.0">    <display-name>FileTest</display-name>    <!-- 配置 Spring MVC DispatchcerServlet 前端控制器 -->    <servlet>        <servlet-name>springmvc</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>/WEB-INF/springmvc-config.xml</param-value>        </init-param>        <load-on-startup>1</load-on-startup>    </servlet>    <!-- Servlet 映射声明 -->    <servlet-mapping>        <servlet-name>springmvc</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <welcome-file-list>        <welcome-file>upload.jsp</welcome-file>    </welcome-file-list></web-app>

2.2 springmvc-config.xml 文件

WebContent/WEB-INF/ 目录下新建 Spring MVC 配置文件 springmvc-config.xml,添加如下代码:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd">    <mvc:annotation-driven />    <context:component-scan base-package="gler.file.controller" />    <bean        class="org.springframework.web.servlet.view.InternalResourceViewResolver">        <property name="prefix" value="/WEB-INF/views/"></property>        <property name="suffix" value=".jsp"></property>    </bean>    <!-- 文件上传的配置 -->    <bean id="multipartResolver"        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">        <!-- 文件上传大小上限 -->        <property name="maxUploadSize">            <value>2000000</value>        </property>        <!-- 编码格式 -->        <property name="defaultEncoding">            <value>UTF-8</value>        </property>    </bean></beans> 

由于 Spring MVC 上下文默认没有装配 MultipartResolver,因此需要配置。

2.3 Controller 类的实现

在包 gler.file.controller 下新建 Controller 类 FileOperateController.java,具体解释注释已经给出,代码如下:

package gler.file.controller;import java.io.File;import javax.servlet.http.HttpServletRequest;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.multipart.MultipartFile;@Controllerpublic class FileOperateController {    @RequestMapping(value="/upload")    public String upload(HttpServletRequest request,@RequestParam("file") MultipartFile file) throws Exception{ // 上传文件会自动绑定到 MultipartFile 中        // 如果文件为空,跳转至失败页面        if(file.isEmpty()){            return "failure";        }else {            // 上传路径            String path = request.getServletContext().getRealPath("/images/");            // 上传文件名            String filename = file.getOriginalFilename();            // 判断路径是否存在,不存在就创建一个            File filepath = new File(path,filename);            if(!filepath.getParentFile().exists())                filepath.getParentFile().mkdirs();            // 将文件保存到一个目标文件中            file.transferTo(new File(path+File.separator+filename));            return "success";        }           }}

2.4 JSP 页面

(1)upload.jsp

WebContent 目录下新建一个 JSP 页面命名为 upload.jsp,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!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=UTF-8"><title>fileupload</title></head><body>    <h4>文件上传</h4>    <form action="upload" method="post" enctype="multipart/form-data">        <table>            <tr>                <td>请选择上传的文件:</td>                <td><input type="file" name="file" /></td>            </tr>            <tr>                <td><input type="submit" value="上传"></td>            </tr>        </table>    </form></body></html>

Spring MVC 为文件上传提供了直接的支持,表单的 method 必须为 post,enctype 为 multipart/form-data

(2)success.jsp

WebContent/WEB-INF 目录下新建文件夹 views,并在该路径下新建一个 JSP 页面命名为 success.jsp,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!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=UTF-8"><title>成功页面</title></head><body><h4>上传成功!</h4></body></html>

(3)failure.jsp

WebContent/WEB-INF/views 目录下新建一个 JSP 页面命名为 failure.jsp,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!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=UTF-8"><title>失败页面</title></head><body><h4>上传失败!</h4></body></html>

2.5 文件上传测试

在 Tomcat 上运行 FileTest,选择一个文件,如 spring.jpeg:

这里写图片描述

点击上传,如果成功跳转至成功页面

这里写图片描述

3 文件下载

我们可以直接修改上面的程序实现文件下载。

3.1 添加 FileInfo 实体类

在项目目录 Java Resources/src 的包 gler.file.entity 下新建类 FileInfo.java,用来绑定上传后的文件信息,代码如下:

package gler.file.entity;import java.io.Serializable;import org.springframework.web.multipart.MultipartFile;public class FileInfo implements Serializable {    private static final long serialVersionUID = 1L;    private MultipartFile file;    public MultipartFile getFile() {        return file;    }    public void setFile(MultipartFile file) {        this.file = file;    }}

3.2 修改 FileOperateController 类

修改 FileOperateController.java,代码如下:

package gler.file.controller;import java.io.File;import javax.servlet.http.HttpServletRequest;import org.apache.commons.io.FileUtils;import org.springframework.http.HttpHeaders;import org.springframework.http.HttpStatus;import org.springframework.http.MediaType;import org.springframework.http.ResponseEntity;import org.springframework.stereotype.Controller;import org.springframework.ui.Model;import org.springframework.web.bind.annotation.ModelAttribute;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import gler.file.entity.FileInfo;@Controllerpublic class FileOperateController {    // 文件上传    @RequestMapping(value="/upload")    public String upload(HttpServletRequest request,@ModelAttribute FileInfo fileinfo,Model model) throws Exception{        if(fileinfo.getFile().isEmpty()){            return "failure";        }else {            // 上传路径            String path = request.getServletContext().getRealPath("/images/");            // 上传文件名            String filename = fileinfo.getFile().getOriginalFilename();            File filepath = new File(path,filename);            if(!filepath.getParentFile().exists())                filepath.getParentFile().mkdirs();            // 将文件保存到一个目标文件中            fileinfo.getFile().transferTo(new File(path+File.separator+filename));            // 将上传的文件信息添加进 model            model.addAttribute("fileinfo",fileinfo);            // 跳转至下载页面            return "download";        }    }    // 文件下载    @RequestMapping(value="/download")    public ResponseEntity<byte[]> download(HttpServletRequest request,@RequestParam("filename") String filename) throws Exception{        // 下载路径        String path = request.getServletContext().getRealPath("/images/");        File file = new File(path+File.separator+filename);        HttpHeaders headers = new HttpHeaders();        // 解决中文乱码        String downloadfile =  new String(filename.getBytes("UTF-8"),"iso-8859-1");        // 以下载方式打开文件        headers.setContentDispositionFormData("attachment", downloadfile);        // 二进制流        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);        return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),headers,HttpStatus.CREATED);    }}

在这里,跳转到下载页面后,显示刚上传文件的下载链接,download 方法获得 jsp 页面的文件名 filename,通过下载路径和文件名构建成 ResponseEntity 对象返回给客户端下载。ResponseEntity 可以方便的定义并返回 HttpHeaders 和 HttpStatus。

3.3 增加下载页面 download.jsp

WebContent/WEB-INF/views 目录下新建一个 JSP 页面命名为 download.jsp,代码如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%><!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=UTF-8"><title>下载页面</title></head><body>    <h4>点击链接进行下载</h4>    <a href="download?filename=${requestScope.fileinfo.file.originalFilename}">${requestScope.fileinfo.file.originalFilename}</a></body></html>

3.4 文件下载测试

在 Tomcat 上运行 FileTest,选择一个文件,如 spring.jpeg:

这里写图片描述

点击上传,如果成功跳转至下载页面

这里写图片描述

点击 spring.jpeg 的下载链接进行下载,浏览器弹出下载框,确定即可下载。

这里写图片描述

参考链接

  • 《Spring+MyBatis 企业应用实战》
  • SpringMVC文件上传与下载
0 0