一、概述

文件上传时,http请求头Content-Type须为multipart/form-data,有两种实现方式:

1、基于FormData对象,该方式简单灵活

2、基于<form>表单元素,method设为POST,enctype设置为multipart/form-data,在form表单上提交

web容器收到该请求时,须根据请求头将字节流解析为文件对象,spring mvc 提供了MultipartResolver、MultipartFile两个接口用于支持文件上传功能

二、MultipartResolver & MultipartFile

1、MultipartResolver接口提供了文件解析功能,其定义如下:

public interface MultipartResolver {
boolean isMultipart(HttpServletRequest request);
MultipartHttpServletRequest resolveMultipart(HttpServletRequest request) throws MultipartException;
void cleanupMultipart(MultipartHttpServletRequest request);
}

Spring MVC使用Apache Commons fileupload技术实现了一个MultipartResolver实现类:CommonsMultipartResolver

2、MultipartFile接口代表上传的文件,提供了文件操作的相关功能,其定义如下:

public interface MultipartFile {
String getName();
String getOriginalFilename(); // 原文件名
String getContentType();
boolean isEmpty();
long getSize();
byte[] getBytes() throws IOException;
InputStream getInputStream() throws IOException; // 获取文件流
void transferTo(File dest) throws IOException, IllegalStateException; // 保存文件
}

三、使用示例

1、添加pom依赖

<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>

2、spring-mvc.xml中配置MultipartResolver

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 上传文件大小限制,单位为字节-10Mb -->
<property name="maxUploadSize">
<value>10485760</value>
</property>
<!-- 请求的编码格式 -->
<property name="defaultEncoding">
<value>UTF-8</value>
</property>
</bean>

3、controller

@Controller
public class FileController {
/**
* 文件存储目录
*/
private static final String uploadFolder = "d:/upload-file/"; /**
* 上传
*/
@ResponseBody
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public Map<String, String> fileUpload(@RequestParam("file") MultipartFile file, @RequestParam("fileType") String fileType) throws Exception { // 创建文件目录
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
String dateStr = sdf.format(new Date());
String dirPath = uploadFolder + dateStr;
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
} // 保存文件
String fileName = UUID.randomUUID() + this.getFileNameSuffix(file.getOriginalFilename());
File savedFile = new File(dir + "/" + fileName);
file.transferTo(savedFile); Map<String, String> result = new HashMap<String, String>();
result.put("fileName", file.getOriginalFilename());
result.put("fileType", fileType);
result.put("filePath", dateStr + "/" + fileName); return result;
} /**
* 下载
*/
@RequestMapping("/download")
public void downloadFile(@RequestParam(value = "fileName", required = false) String fileName, @RequestParam("filePath") String filePath, HttpServletResponse response) throws Exception {
File file = new File(uploadFolder + filePath);
if (!file.exists()) {
return;
} // 读取字节流到缓存
InputStream in = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[in.available()];
in.read(buffer);
in.close(); // 设置ContentType
String suff = this.getFileNameSuffix(file.getName());
if (suff != null) {
suff = suff.toLowerCase();
}
switch (suff) {
case ".jpg":
case ".jpeg":
response.setContentType(MediaType.IMAGE_JPEG_VALUE);
break;
case ".png":
response.setContentType(MediaType.IMAGE_PNG_VALUE);
break;
case ".gif":
response.setContentType(MediaType.IMAGE_GIF_VALUE);
break;
default:
response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
break;
} // 输出
response.addHeader("Content-Disposition", "attachment;filename=\"" + (StringUtils.isEmpty(fileName) ? file.getName() : fileName));
response.addHeader("Content-Length", "" + file.length());
response.setContentType("application/x-msdownload");
OutputStream out = new BufferedOutputStream(response.getOutputStream());
out.write(buffer);
out.flush();
} /**
* 获取文件名后缀*/
private String getFileNameSuffix(String fileName) { if (StringUtils.isEmpty(fileName)) {
return null;
} String suffix;
int index = fileName.lastIndexOf(".");
if (index == -1) {
suffix = null;
}
else {
suffix = fileName.substring(index);
} return suffix;
}
}

4、前端页面(test.html)

<!DOCTYPE html>
<html>
<head>
<title>test page</title>
</head>
<body>
<div>
<input id="upload" type="file" onchange="uploadFile(event)" />
</div>
<div>
<input id="download" type="button" onclick="downloadFile()" value="下载" />
</div>
</body>
<script>
var context = '/test' function uploadFile(event) {
// 文件类型过滤
var file = event.currentTarget.files[0];
var fileName = file.name;
if (/\.(jpg|jpeg|png|gif)$/i.test(fileName) == false) {
alert('提示', '不支持的图片类型,头像只支持.jpg,.jpeg,.png,.gif');
return;
} var formData = new FormData();
formData.append('fileType', 'portrait');
formData.append('file', event.currentTarget.files[0]); // 发送请求
var xhr = new XMLHttpRequest();
xhr.open('POST', context + '/upload', true);
xhr.onreadystatechange = function() {
if (xhr.readyState==4 && xhr.status==200) {
document.getElementById('download').fileData = JSON.parse(xhr.responseText);
alert("上传成功");
}
}
xhr.send(formData); // 重置文件上传控件,使得重复选择同一个文件时,onchange依旧触发
event.target.value = null;
} function downloadFile() {
var fileData = document.getElementById('download').fileData;
location.href = context + '/download?filePath=' + decodeURIComponent(fileData.filePath) + '&fileName=' + decodeURIComponent(fileData.fileName);
}
</script>
</html>

访问http://file-test/test.html,可测试文件的上传下载功能

补充:示例使用@ResponseBody输出json数据,因此还需添加相关配置,详细可参考Spring MVC 使用介绍(五)—— 注解式控制器(一):基本介绍

另外,文件下载除了示例中文件流方式,还可以基于spring对静态文件的支持功能,详细可参考Spring MVC 使用介绍(十一)—— 跨域与静态资源访问

参考:

Spring MVC的文件上传

Spring MVC 使用介绍(十四)文件上传下载的更多相关文章

  1. JavaWeb 后端 <十四> 文件上传下载

    1.文件上传与下载 案例: 注册表单/保存商品等相关模块! --à 注册选择头像 / 商品图片 (数据库:存储图片路径 / 图片保存到服务器中指定的目录) 1.1 文件上传 文件上传,要点: 前台: ...

  2. 使用Typescript重构axios(二十五)——文件上传下载进度监控

    0. 系列文章 1.使用Typescript重构axios(一)--写在最前面 2.使用Typescript重构axios(二)--项目起手,跑通流程 3.使用Typescript重构axios(三) ...

  3. Spring MVC使用commons fileupload实现文件上传功能

    通过Maven建立Spring MVC项目,引入了Spring相关jar依赖. 1.为了使用commons fileupload组件,需要在pom.xml中添加依赖: <properties&g ...

  4. SpringMVC 文件上传下载

    目录 文件上传 MultipartFile对象 文件下载 上传下载示例 pom.xml增加 创建uploadForm.jsp 创建uploadForm2.jsp 创建userInfo.jsp spri ...

  5. Spring Boot2(十四):单文件上传/下载,文件批量上传

    文件上传和下载在项目中经常用到,这里主要学习SpringBoot完成单个文件上传/下载,批量文件上传的场景应用.结合mysql数据库.jpa数据层操作.thymeleaf页面模板. 一.准备 添加ma ...

  6. salesforce 零基础学习(四十二)简单文件上传下载

    项目中,常常需要用到文件的上传和下载,上传和下载功能实际上是对Document对象进行insert和查询操作.本篇演示简单的文件上传和下载,理论上文件上传后应该将ID作为操作表的字段存储,这里只演示文 ...

  7. MVC&WebForm对照学习:文件上传(以图片为例)

    原文  http://www.tuicool.com/articles/myM7fe 主题 HTMLMVC模式Asp.net 博客园::首页::  ::  ::  ::管理 5 Posts :: 0 ...

  8. SpringMVC文件上传下载(单文件、多文件)

    前言 大家好,我是bigsai,今天我们学习Springmvc的文件上传下载. 文件上传和下载是互联网web应用非常重要的组成部分,它是信息交互传输的重要渠道之一.你可能经常在网页上传下载文件,你可能 ...

  9. SpringBoot入门一:基础知识(环境搭建、注解说明、创建对象方法、注入方式、集成jsp/Thymeleaf、logback日志、全局热部署、文件上传/下载、拦截器、自动配置原理等)

    SpringBoot设计目的是用来简化Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置.通过这种方式,SpringBoot致力于在蓬勃发 ...

  10. SpringMVC文件上传下载

    在Spring MVC的基础框架搭建起来后,我们测试了spring mvc中的返回值类型,如果你还没有搭建好springmvc的架构请参考博文->http://www.cnblogs.com/q ...

随机推荐

  1. kubernetes系列10—存储卷详解

    本文收录在容器技术学习系列文章总目录 1.认识存储卷 1.1 背景 默认情况下容器中的磁盘文件是非持久化的,容器中的磁盘的生命周期是短暂的,这就带来了一系列的问题:第一,当一个容器损坏之后,kubel ...

  2. Centos7配置MongoDB以及端口修改

    一.安装之前可以参考官网安装教程: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat/ 二.登录服务器,配置yum ...

  3. ASP.Net Mvc实现自定义User Identity用户身份识别系统(1)

    目的 当我们新建asp.net mvc 项目时,我们在使用下图所示的代码是否有以下思考: 1,在this.User.Identity.Name,为什么可以使用this便可以选中Name属性: 2,若项 ...

  4. 从QA到工程能效团队

    Engineering Productivity Productivity is our job; testing and quality are the job of everyone involv ...

  5. cisco 下CEF(简介)[转载]

    cisco 下CEF(简介) 转自:http://blog.51cto.com/chengmingshu/803358 cms_cmd1人评论2570人阅读2012-03-12 00:09:29   ...

  6. OPPO A7x在哪里开启usb调试模式的详细经验

    当我们使用Pc连接安卓手机的时候,如果手机没有开启Usb调试模式,Pc则没法成功读到我们的手机,这时我们需要想办法将手机的Usb调试模式开启,这里我们讲解OPPO A7x如何开启Usb调试模式的方法. ...

  7. SpringBoot+MyBatis配置多数据源

    SpringBoot 可以支持多数据源,这是一个非常值得学习的功能,但是从现在主流的微服务的架构模式中,每个应用都具有唯一且准确的功能,多数据源的需求很难用到,考虑到实际情况远远比理论复杂的多,这里还 ...

  8. IDEA启动tomcat乱码

    1.找到IDEA安装目录 2.找到2个文件 3.编辑,在最后一行加入 -Dfile.encoding=UTF-8 4.修改IDEA里tomcat内得编码 5.修改IDEA中tomcat中,startu ...

  9. SQL Server 事务日志文件已满,收缩日志文件(9002)

    错误如下图: 1.数据库 → 属性 → 选项 → 恢复模式 → 选择‘简单’:如下图: 2.任务 → 收缩 → 文件类型‘文件’ → 收缩模式‘在释放未使用的空间前重新组织页’,将文件收缩到K,大小填 ...

  10. 经典51道SQL查询练习题

    数据表介绍 --1.学生表 Student(SId,Sname,Sage,Ssex) --SId 学生编号,Sname 学生姓名,Sage 出生年月,Ssex 学生性别 --2.课程表 Course( ...