SpringMVC 中,文件的上传,是通过 MultipartResolver 实现的。 所以,如果要实现文件的上传,只要在 spring-mvc.xml 中注册相应的 MultipartResolver 即可。
MultipartResolver 的实现类有两个:
- CommonsMultipartResolver
- StandardServletMultipartResolver
两个的区别:
- 第一个需要使用 Apache 的 commons-fileupload 等 jar 包支持,但它能在比较旧的 servlet 版本中使用。
- 第二个不需要第三方 jar 包支持,它使用 servlet 内置的上传功能,但是只能在 Servlet 3 以上的版本使用。
第一个使用步骤:
- /*CommonsMultipartResolver 上传用到的两个包*/
"commons-fileupload:commons-fileupload:1.3.1",
"commons-io:commons-io:2.4"
如果是maven项目的话直接导入:
- <dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
dispatcher-servlet.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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
-
- <context:component-scan base-package="edu.nf.ch08.controller"/>
-
- <mvc:annotation-driven/>
-
- <mvc:default-servlet-handler/>
- <!-- 文件上传有两种方式,一种基于Servlet3.0的上传,一种基于
- commons-upload上传,如果使用Servlet3.0的上传方式,可以
- 不需要配置MultipartResolver,Spring默认会注册一个
- StandardServletMultipartResolver。只需要在web.xml中
- 启用<multipart-config>。
- 如果想使用commons-upload,那么需要配置一个CommonsMultipartResolver,
- 且指定bean的id为multipartResolver-->
- <!-- 这里使用commons-upload-->
- <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <!-- 限制文件上传的总大小(单位:字节),不配置此属性默认不限制 -->
- <property name="maxUploadSize" value="104857600"/>
- <!-- 设置文件上传的默认编码-->
- <property name="defaultEncoding" value="utf-8"/>
- </bean>
-
- <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
- <property name="prefix" value="/WEB-INF/jsp/"/>
- <property name="suffix" value=".jsp"/>
- </bean>
- </beans>
web.xml:
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
- version="4.0">
-
- <!-- 请求总控器 -->
- <servlet>
- <servlet-name>dispatcher</servlet-name>
- <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
- <init-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath:dispatcher-servlet.xml</param-value>
- </init-param>
- </servlet>
- <servlet-mapping>
- <servlet-name>dispatcher</servlet-name>
- <url-pattern>/</url-pattern>
- </servlet-mapping>
-
- </web-app>
后台java(上传、下载)处理代码:
- package edu.nf.ch08.controller;
- 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.web.bind.annotation.GetMapping;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.multipart.MultipartFile;
- import org.springframework.web.servlet.ModelAndView;
- import java.io.File;
- import java.io.IOException;
- /**
- * @author wangl
- * @date 2018/11/2
- */
- @Controller
- public class UploadController {
- /**
- * 文件上传只需要Spring传入一个MultipartFile对象即可,
- * 这个对象可以获取文件相关上传的信息。
- * 一个MultipartFile表示单个文件上传,当需要上传多个文件时
- * 只需要声明为MultipartFile[]数组即可。
- * @return
- */
- @PostMapping("/upload")
- public ModelAndView upload(MultipartFile file){
- //获取当前系统用户目录
- String home = System.getProperty("user.home");
- //指定上传的文件夹目录
- File uploadDir = new File(home + "/files");
- //如果目录不存在,则创建
- if(!uploadDir.exists()){
- uploadDir.mkdir();
- }
- //获取上传的文件名
- String fileName = file.getOriginalFilename();
- //构建一个完整的文件上传对象
- File uploadFile = new File(uploadDir.getAbsolutePath() + "/" + fileName);
- try {
- //通过transferTo方法进行上传
- file.transferTo(uploadFile);
- } catch (IOException e) {
- e.printStackTrace();
- throw new RuntimeException(e.getMessage());
- }
- //将文件名存入Model,转发到index页面
- ModelAndView mv = new ModelAndView("index");
- mv.addObject("fileName", fileName);
- return mv;
- }
- /**
- * 文件下载
- * 读取服务器本地文件并封装为ResponseEntity对象
- * 响应客户端,写回的是一个字节数组
- * @param fileName 文件名
- * @return
- */
- @GetMapping("/download")
- public ResponseEntity<byte[]> download(String fileName){
- //依据文件名构建本地文件路径
- String filePath = System.getProperty("user.home") + "/files/" + fileName;
- //依据文件路径构建File对象
- File file = new File(filePath);
- //创建响应头对象,设置响应信息
- HttpHeaders headers = new HttpHeaders();
- try {
- //对文件名进行重新编码,防止在响应头中出现中文乱码
- String headerFileName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");
- //设置响应内容处理方式为附件,并指定文件名
- headers.setContentDispositionFormData("attachment", headerFileName);
- //设置响应头类型为application/octet-stream,表示是一个流类型
- headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
- //将文件转换成字节数组
- byte[] bytes = FileUtils.readFileToByteArray(file);
- //创建ResponseEntity对象(封装文件字节数组、响应头、响应状态码)
- ResponseEntity<byte[]> entity = new ResponseEntity<>(bytes, headers, HttpStatus.CREATED);
- //最后将整个ResponseEntity对象返回给DispatcherServlet
- return entity;
- } catch (Exception e) {
- e.printStackTrace();
- throw new RuntimeException("文件下载失败");
- }
- }
- }
上传文件的网页html:
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <title>Title</title>
- </head>
- <body>
- <h1>文件上传</h1>
- <!-- 当有文件上传时,表单的enctype必须设置为multipart/form-data -->
- <form method="post" action="upload" enctype="multipart/form-data">
- File:<input type="file" name="file"/><br/>
- <input type="submit" value="submit"/>
- </form>
- </body>
- </html>

上传成功后转发的jsp(下载文件)页面:
- <%--
- Created by IntelliJ IDEA.
- User: wangl
- Date: 2018/11/2
- Time: 09:56
- To change this template use File | Settings | File Templates.
- --%>
- <%@ page contentType="text/html;charset=UTF-8" language="java" %>
- <html>
- <head>
- <title>Title</title>
- </head>
- <body>
- <a href="download?fileName=${fileName}">${fileName}</a>
- </body>
- </html>

项目结构:
