SpringMVC实现文件的上传和下载实例代码

时间:2019-03-30
本文章向大家介绍SpringMVC实现文件的上传和下载实例代码,主要包括SpringMVC实现文件的上传和下载实例代码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前些天一位江苏经贸的学弟跟我留言问了我这样一个问题:“用什么技术来实现一般网页上文件的上传和下载?是框架还是Java中的IO流”。我回复他说:“使用SpringMVC框架可以做到这一点,因为SpringMVC为文件的上传提供了直接的支持,但需要依赖Apache提供Commons FileUpload组件jar包。”鉴于这个问题,我上网也百度了一下,网上很多都是介绍的使用IO流来实现文件的上传和下载,也有说到框架的,但介绍的并不是很完整,今天小钱将和大家介绍使用SpringMVC框架来实现文件的上传和下载,本文将做一个项目案例来完整的使用SpringMVC框架实现文件的上传和下载。

文件上传

文件上传是项目开发中最常见的功能。为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data。只有在这样的情况下,浏览器才会把用户选择的文件以二进制数据发送给服务器。

一旦设置了enctype为multipart/form-data,浏览器即会采用二进制流的方式来处理表单数据,而对于文件上传的处理则涉及在服务器端解析原始的HTTP响应。在2003年,Apache Software Foundation发布了开源的Commons FileUpload组件,其很快成为Servlet/JSP程序员上传文件的最佳选择。

Servlet3.0规范已经提供方法来处理文件上传,但这种上传需要在Servlet中完成。而SpringMVC则提供了更简单的封装。
SpringMVC为文件上传提供了直接的支持,这种支持是用即插即用的MultipartResolver实现的。SpringMVC使用Apache Commons FileUpload技术实现了一个MultipartResolver实现类:CommonsMultipartResolver。因此,SpringMVC的文件上传还需要依赖Apache Commons FileUpload的组件。

本项目作为测试案例,在此我就不创建Maven项目了,我直接创建的是一个Dynamic Web Project(动态的web项目),采用Tomcat 8作为web服务器,我们需要在项目中引入以下jar包,如下图:

下面我们在WebContent/WEB-INF下创建一个content文件夹,用于放文件的上传、下载等jsp文件,下面我们创建uploadForm.jsp文件,演示SpringMVC的文件上传:

<%@ 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>
  <h2>文件上传</h2>
  <form action="upload" enctype="multipart/form-data" method="post">
    <table>
      <tr>
        <td>文件描述:</td>
        <td><input type="text" name="description"></td>
      </tr>
      <tr>
        <td>请选择文件:</td>
        <td><input type="file" name="file"></td>
      </tr>
      <tr>
        <td><input type="submit" value="上传"></td>
      </tr>
    </table>
  </form>
</body>
</html>

负责上传文件的表单和一般表单有一些区别,负责上传文件的表单的编码类型必须是“multipart/form-data”。

我们再src下创建一个包“cn.edu.jseti.controller”,然后创建一个FileUploadController类,用于实现文件的上传和下载功能。以下是负责上传文件的表单功能代码:

//上传文件会自动绑定到MultipartFile中
   @RequestMapping(value="/upload",method=RequestMethod.POST)
   public String upload(HttpServletRequest request,
      @RequestParam("description") String description,
      @RequestParam("file") MultipartFile file) throws Exception {

    System.out.println(description);
    //如果文件不为空,写入上传路径
    if(!file.isEmpty()) {
      //上传文件路径
      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";
    } else {
      return "error";
    }

   }

SpringMVC会将上传的文件绑定到MultipartFile对象中。MultipartFile提供了获取上传文件内容、文件名等方法。通过transferTo()方法还可以将文件存储到硬件中,MultipartFile对象中的常用方法如下:

  1. byte[] getBytes():获取文件数据
  2. String getContentType[]:获取文件MIME类型,如image/jpeg等
  3. InputStream getInputStream():获取文件流
  4. String getName():获取表单中文件组件的名字
  5. String getOriginalFilename():获取上传文件的原名
  6. Long getSize():获取文件的字节大小,单位为byte
  7. boolean isEmpty():是否有上传文件
  8. void transferTo(File dest):将上传文件保存到一个目录文件中

SpringMVC上下文中默认没有装配MultipartResolver,因此默认情况下其不能处理文件上传工作。如果想使用spring的文件上传功能,则需要在上下文中配置MultipartResolver。在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:p="http://www.springframework.org/schema/p"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd   
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">

  <!-- spring可以自动去扫描base-pack下面的包或者子包下面的java文件,
    如果扫描到有Spring的相关注解的类,则把这些类注册为Spring的bean -->
  <context:component-scan base-package="cn.edu.jseti.controller"/>

  <!-- 视图解析器 -->
   <bean id="viewResolver"
     class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <!-- 前缀 -->
    <property name="prefix">
      <value>/WEB-INF/content/</value>
    </property>
    <!-- 后缀 -->
    <property name="suffix">
      <value>.jsp</value>
    </property>
  </bean>

  <bean id="multipartResolver" 
    class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> 
    <!-- 上传文件大小上限,单位为字节(10MB) -->
    <property name="maxUploadSize"> 
      <value>10485760</value> 
    </property> 
    <!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
    <property name="defaultEncoding">
      <value>UTF-8</value>
    </property>
  </bean>

</beans>

部署FileUpload这个Web应用,在浏览器中输入如下URL来测试应用: http://localhost:8088/FileUpload/uploadForm

输入文件描述信息并选择上传文件,如下图:

 

点上传按钮,这是已将上传的文件通过二进制保存到web服务器上去了,如下图:

使用对象接收上传文件

上面我们通过案例演示了SpringMVC上传文件,接下来,我们演示使用对象接收上传文件。

在实际项目的开发中,很多时候上传的文件会作为对象的属性被保存。SpringMVC的处理也非常的简单。

下面我们在content文件夹创建registerForm.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>
  <h2>用户注册</h2>
  <form action="register" enctype="multipart/form-data" method="post">
    <table>
      <tr>
        <td>用户名:</td>
        <td><input type="text" name="username"></td>
      </tr>
      <tr>
        <td>请上传头像:</td>
        <td><input type="file" name="image"></td>
      </tr>
      <tr>
        <td><input type="submit" value="注册"></td>
      </tr>
    </table>
  </form>
</body>
</html>

我们在src下面创建一个名叫“cn.edu.jseti.domain”包,然后再创建一个User类,必须要实现序列化接口,如下案例代码:

package cn.edu.jseti.domain;
import java.io.Serializable;

import org.springframework.web.multipart.MultipartFile;

/**
 * 博客:http://blog.csdn.net/qian_ch
 * @author Cody
 * @version V1.0 
 */

//域对象,实现序列化接口
public class User implements Serializable{

  private String username;
  private MultipartFile image;

  public User() {
    super();
  }

  public String getUsername() {
    return username;
  }

  public void setUsername(String username) {
    this.username = username;
  }

  public MultipartFile getImage() {
    return image;
  }

  public void setImage(MultipartFile image) {
    this.image = image;
  }  

}

我们在刚才创建的FileUploadController类继续写用于接收文件的上传和下载功能。以下是负责接收文件的表单功能代码:

   @RequestMapping(value="/register")
   public String register(HttpServletRequest request,
       @ModelAttribute User user,
       Model model) throws Exception {
    System.out.println(user.getUsername());
    //如果文件不为空,写入上传路径
    if(!user.getImage().isEmpty()) {
      //上传文件路径
      String path = request.getServletContext().getRealPath("/images/");
      //上传文件名
      String filename = user.getImage().getOriginalFilename();
      File filepath = new File(path,filename);
      //判断路径是否存在,如果不存在就创建一个
      if (!filepath.getParentFile().exists()) { 
        filepath.getParentFile().mkdirs();
      }
      //将上传文件保存到一个目标文件当中
      user.getImage().transferTo(new File(path + File.separator + filename));
      //将用户添加到model
      model.addAttribute("user", user);
      return "userInfo";
    } else {
      return "error";
    }
  }

在content文件夹下创建userInfo.jsp文件,该页面主要是文件的下载页面,如下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>
<h3>文件下载</h3>
<a href="download?filename=${requestScope.user.image.originalFilename}" rel="external nofollow" >
  ${requestScope.user.image.originalFilename }
</a>
</body>
</html>

在浏览器中输入如下URL来测试应用: http://localhost:8088/FileUpload/registerForm

输入用户名并上传刚才上传的文件。如下图:

 

单击“注册”按钮上传文件,然后就会跳转到下载页面。如下图:

文件下载

上面我们通过案例演示了使用对象接收上传文件,接下来,我们演示SpringMVC的下载文件。

文件下载比较简单,直接在页面给出了一个超链接,该链接href的属性等于要下载文件的文件名,就可以实现文件下载了。但是如果该文件的文件名为中文文件名,在某些早起的浏览器上就会导致下载失败;如果使用最新的Firefox、Chrome、Opera、Safari则都可以正常下载文件名为中文的文件了。

SpringMVC提供了一个ResponseEntity类型,使用它可以很方便地定义返回的HttpHeaders和HttpStatus。以下代码演示文件的下载功能:

@RequestMapping(value="/download")
   public ResponseEntity<byte[]> download(HttpServletRequest request,
       @RequestParam("filename") String filename,
       Model model)throws Exception {
    //下载文件路径
    String path = request.getServletContext().getRealPath("/images/");
    File file = new File(path + File.separator + filename);
    HttpHeaders headers = new HttpHeaders(); 
    //下载显示的文件名,解决中文名称乱码问题 
    String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
    //通知浏览器以attachment(下载方式)打开图片
    headers.setContentDispositionFormData("attachment", downloadFielName); 
    //application/octet-stream : 二进制流数据(最常见的文件下载)。
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),  
        headers, HttpStatus.CREATED); 
   }

download处理方法接收页面传递的文件名filename后,使用Apache Commons FileUpload组件的FileUtils读取项目的上传文件,并将其构建成ResponseEntity对象返回客户端下载。

使用ResponseEntity对象,可以很方便的定义返回的HttpHeaders和HttpStatus。上面代码中的MediaType,代表的是Internet Media Type,即互联网媒体类型,也叫做MIME类型。在Http协议消息头中,使用Content-Type来表示具体请求中的媒体类型信息。HttpStatus类型代表的是Http协议中的状态。有关MediaType和HttpStatus类可以参考SpringMVC的API文档。

点击下载页面的超链接,显示文件正在下载,如下图所示:

单击“浏览”按钮,可以选择下载文件的保存路径,然后单击“确定”按钮,文件就会顺利的下载并保存。

本文中的一些功能案例代码和配置文件不是很完整,下面附上完整代码:

FileUploadController类完整的代码如下:

package cn.edu.jseti.controller;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import cn.edu.jseti.domain.User;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
/**
 * @author Cody
 * @version V1.0 
 */
@Controller
public class FileUploadController {
  @RequestMapping(value="/{formName}")
   public String loginForm(@PathVariable String formName) {
    // 动态跳转页面
    return formName;
  }

  //上传文件会自动绑定到MultipartFile中
   @RequestMapping(value="/upload",method=RequestMethod.POST)
   public String upload(HttpServletRequest request,
      @RequestParam("description") String description,
      @RequestParam("file") MultipartFile file) throws Exception {

    System.out.println(description);
    //如果文件不为空,写入上传路径
    if(!file.isEmpty()) {
      //上传文件路径
      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";
    } else {
      return "error";
    }

   }

   @RequestMapping(value="/register")
   public String register(HttpServletRequest request,
       @ModelAttribute User user,
       Model model) throws Exception {
    System.out.println(user.getUsername());
    //如果文件不为空,写入上传路径
    if(!user.getImage().isEmpty()) {
      //上传文件路径
      String path = request.getServletContext().getRealPath("/images/");
      //上传文件名
      String filename = user.getImage().getOriginalFilename();
      File filepath = new File(path,filename);
      //判断路径是否存在,如果不存在就创建一个
      if (!filepath.getParentFile().exists()) { 
        filepath.getParentFile().mkdirs();
      }
      //将上传文件保存到一个目标文件当中
      user.getImage().transferTo(new File(path + File.separator + filename));
      //将用户添加到model
      model.addAttribute("user", user);
      return "userInfo";
    } else {
      return "error";
    }
  }

   @RequestMapping(value="/download")
   public ResponseEntity<byte[]> download(HttpServletRequest request,
       @RequestParam("filename") String filename,
       Model model)throws Exception {
    //下载文件路径
    String path = request.getServletContext().getRealPath("/images/");
    File file = new File(path + File.separator + filename);
    HttpHeaders headers = new HttpHeaders(); 
    //下载显示的文件名,解决中文名称乱码问题 
    String downloadFielName = new String(filename.getBytes("UTF-8"),"iso-8859-1");
    //通知浏览器以attachment(下载方式)打开图片
    headers.setContentDispositionFormData("attachment", downloadFielName); 
    //application/octet-stream : 二进制流数据(最常见的文件下载)。
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file),  
        headers, HttpStatus.CREATED); 
   }

}

文件上传成功的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>
  恭喜,您的上传文件成功!
</body>
</html>

文件上传失败的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>
  上传文件失败!
</body>
</html>

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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>MultipartFileTest</display-name>

 <!-- 定义Spring MVC的前端控制器 -->
 <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>

 <!-- 让Spring MVC的前端控制器拦截所有请求 -->
 <servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

 <!-- 编码过滤器 -->
 <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
 </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

OK,以上就是完整的使用SpringMVC框架实现了文件的上传和下载。本中所有的案例都是本人亲自测试,如有写的不对,欢迎朋友们留言一起交流,谢谢!

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。