SpringBoot 文件上传实践
背景:将上传的文件,如图片,写入指定服务器路径,保存起来。多文件上传时,由于HttpServletRequest不能直接取出文件数据,所以将其强制转换为MultipartHttpServletRequest。本文使用Postman模拟表单提交。
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
Map<String,MultipartFile> files = multipartRequest.getFileMap();
后台实现:
/**
* @Title uploadFiles
* @Description 测试多文件上传
* @date 2018-11-10 10:15
*/
@PostMapping("/uploadFiles")
public Map<String, Object> uploadFiles(HttpServletRequest req) {
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) req;
// 无须知道上传时文件对应的key,遍历处理
Map<String, MultipartFile> files = multipartRequest.getFileMap();
String otherParam = req.getParameter("otherParam");
Map<String, Object> data = new HashMap<>();
for (MultipartFile value : files.values()) {
data = uploadFile(value);
int code = (int) data.get("code");
if (200 != code) {
return data;
}
}
data.put("code", 200);
data.put("result", otherParam);
return data;
} /**
* @Title uploadFile
* @Description 逐个上传
* @date 2018-11-10 10:17
*/
private Map<String, Object> uploadFile(MultipartFile file) {
Map<String, Object> result = new HashMap<>();
// 判断文件是否为空
if (file.isEmpty()) {
result.put("code", -1);
return result;
}
String fileName = file.getOriginalFilename();
// 原文件名前加时间戳和随机数,避免覆盖文件
String path = "D:/temp/" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
path = path + ((int) Math.random() * 999) + "_" + fileName;
File dest = new File(path);
if (dest.exists()) {
result.put("code", -2);
return result;
}
// 判断文件父目录是否存在
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdir();
}
try {
file.transferTo(dest); // 保存文件
} catch (IOException e) {
result.put("code", -3);
return result;
}
result.put("code", 200);
return result;
}
Postman模拟提交时,配置如图:

环境:springBootVersion 版本 '1.5.3.RELEASE'。
参考文献:https://www.cnblogs.com/chevin/p/9260842.html。
SpringBoot 文件上传实践的更多相关文章
- 补习系列(11)-springboot 文件上传原理
目录 一.文件上传原理 二.springboot 文件机制 临时文件 定制配置 三.示例代码 A. 单文件上传 B. 多文件上传 C. 文件上传异常 D. Bean 配置 四.文件下载 小结 一.文件 ...
- 【SpringBoot】07.SpringBoot文件上传
SpringBoot文件上传 1.编写html文件在classpath下的static中 <!DOCTYPE html> <html> <head> <met ...
- SpringBoot 文件上传临时文件路径问题
年后放假回来,一向运行OK的项目突然图片上传不了了,后台报错日志如下: java.io.IOException: The temporary upload location [/tmp/tomcat. ...
- springboot文件上传下载简单使用
springboot的文件上传比较简单 一.使用默认的Resolver:StandardServletMultipartResolver controller package com.mydemo.w ...
- springboot 文件上传大小配置
转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...
- SpringBoot文件上传下载
项目中经常会有上传和下载的需求,这篇文章简述一下springboot项目中实现简单的上传和下载. 新建springboot项目,前台页面使用的thymeleaf模板,其余的没有特别的配置,pom代码如 ...
- Springboot 文件上传(带进度条)
1. 相关依赖 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- SpringBoot 文件上传、下载、设置大小
本文使用SpringBoot的版本为2.0.3.RELEASE 1.上传单个文件 ①html对应的提交表单 <form action="uploadFile" method= ...
- SpringBoot文件上传异常之提示The temporary upload location xxx is not valid
原文: 一灰灰Blog之Spring系列教程文件上传异常原理分析 SpringBoot搭建的应用,一直工作得好好的,突然发现上传文件失败,提示org.springframework.web.multi ...
随机推荐
- composer 再centos 下的安装
$ curl -sS https://getcomposer.org/installer | php $ sudo mv composer.phar /usr/local/bin/composer
- easyui-combox(tagbox) 多选操作 显示为tagbox
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用Android拨打电话功能
1.要使用Android系统中的电话拨号功能,首先必须在AndroidManifest.xml功能清单中加入允许拨打电话的权限: <uses-permission android:name=&q ...
- [Algorithm] *String Matching and Hashing
Link: Computer Algorithms: Rabin-Karp String Searching 为了避免挨个字符对文本和模式串进行比较,我们可以尝试一次性判断两者是否相等. 因此,我们需 ...
- Linux内核 GPIO操作部分API
内核中关于GPIO的操作API主要集中在<linux/of_gpio.h>和<linux/gpio.h>中,前者主要是GPIO直接与设备树相关的操作,在Linux 设备树操作A ...
- css3整理--background-size
background-size语法: /*Mozilla*/ -moz-background-size: auto || <length> || <percentage> || ...
- [原]openstack-kilo--issue(十)ERROR: openstack Unable to establish connection to http://controller:35357/v3/auth/tokens
====环境== openstack :kilo CentOS : 7 ====问题=== 在没有关vm的情况下,重启了controller. 问题一: 在使用nova service-list 或者 ...
- iOS - UILabel添加图片之富文本的简单应用
//创建富文本 NSMutableAttributedString *attri = [[NSMutableAttributedString alloc] initWithString:@" ...
- Python变量访问权限控制
oop1.py文件代码 # user/bin/python class Foo: def bar(self): print('ok') def hello(self, name): print(&qu ...
- db first和code first
1. db first 是现有数据库,再写代码.根据数据库的表生成类. django里面:python manage.py inspectdb 2. code first 是先写代码,后创建数据库. ...