一、概述

文件上传时,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. sublime text3插件解决输入法不跟随的问题

    快捷键ctrl + shift +p 输入  install package 回车,调出插件搜索器, 在搜索栏中输入 IMESupport 回车安装插件. 即可解决问题.

  2. nginx rewrite 实现URL跳转

    最近工作中常常要改nginx配置,学习了nginx中rewrite的用法 URL跳转这里说的URL跳转就是用户在访问一个URL时将其跳转到另一个URL上.常见的应用场景是让多个域名跳转到同一个URL上 ...

  3. Linux网络相关概念和修改IP地址的方法

    2.1.1  网卡的命名规则 CENTOS6的网卡命名方式:它会根据情况有所改变而非唯一且固定,在CENTOS6之前,网络接口使用连续号码命名: eth0. eth1等,当增加或删除网卡时,名称可能会 ...

  4. android WebView技术笔记(存储技术)

    作为刚刚开始工作的新手程序猿,各种知识还不算熟练,所以将学习到的知识记录一下以免以后忘记. WebView加载h5的知识现在可以说非常的常用,通过加载h5的网页可以在不更新app的情况下来更新app的 ...

  5. mac git从代码仓库克隆代码,修改并上传

    1:添加本地秘钥到代码仓库中 open ~/ .ssh 以github为例: mac 命令行输入open ~/ .ssh,打开id_rsa.pub文件中的内容,复制到github->settin ...

  6. rocketmq简单消息发送

    有以下3种方式发送RocketMQ消息 可靠同步发送 reliable synchronous 可靠异步发送 reliable asynchronous 单向发送 one-way transmissi ...

  7. js坚持不懈之16:使用js向HTML元素分配事件

    向 button 元素分配 onclick 事件: <!DOCTYPE html> <html> <body> <p>点击按钮就可以执行 <em& ...

  8. 使用bootstrap的dropdown部件时报错:error:Bootstrap dropdown require Popper.js

    前言:前端小白一枚,刚注册博客,先发个学习过程中新碰到小问题试试水吧~ 摘要:最近在学习bootstrap,偶然碰到了一个小问题,bootstrap网站也没有做过多的解释,今天分享给大家. 问题描述: ...

  9. 搭建Hadoop的环境

    准备实验的环境: 1.安装Linux.JDK 2.配置主机名.免密码登录 3.约定:安装目录:/usr/local/bin 安装: 1.解压 : tar -zxvf hadoop-2.7.7.tar. ...

  10. vuetify

    vue add vuetify打包之后再发过来另外剪头发 typescript 1.配置 2.当用 let 声明一个变量,它使用的是词法作用域或块作用域. let与var区别,所以要--- 3.看来 ...