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 ...
随机推荐
- too many open files
压测遇到这个问题,每次都查,记录一下: 系统分配文件数太少,临时修改方案: ulimit -n 2048 永久配置: vim /etc/security/limits.conf 底部配置: # End ...
- 模板, 保存&发布
单击“视图”菜单中的“幻灯片模板”按钮,会弹出“幻灯片母版”选项卡,在此选项卡中,可以修改当前PPT的模板, 例如网上下载的幻灯片上的LOGO都可在这里去除. 模板设计总结 1. 背景可以选择纯色,也 ...
- Android应用图标微技巧,8.0系统中应用图标的适配
现在已经进入了2018年,Android 8.0系统也逐渐开始普及起来了.三星今年推出的最新旗舰机Galaxy S9已经搭载了Android 8.0系统,紧接着小米.华为.OV等国产手机厂商即将推出的 ...
- Mac/Linux如何查找应用所安装路径
Linux.Mac中查看某 个软件的安装路径(地址)有时显得非常重要.比如某个文件的快速启动项被删除,或者你要建立快速启动项,或者想删除. 添加安装文件等等,很多地方都要用到查案文件安装路径的命令. ...
- Go指南练习_映射
源地址 https://tour.go-zh.org/moretypes/23 一.题目描述 实现 WordCount.它应当返回一个映射,其中包含字符串 s 中每个“单词”的个数.函数 wc.Tes ...
- Go指南_指针接收者
源地址 https://tour.go-zh.org/methods/4 一.描述 你可以为指针接收者声明方法. 这意味着对于某类型 T,接收者的类型可以用 *T 的文法.(此外,T 不能是像 *in ...
- 怎样利用Heartbeat与Floating IP在Ubuntu 14.04上创建高可用性设置
提供 ZStack社区 内容简单介绍 Heartbeat是一款开源程序,负责将集群基础设施容量--包括集群成员与消息收发--交付至客户server. Hearbeat在高可用性server基础设施其中 ...
- 【代码审计】XYHCMS V3.5任意文件下载漏洞分析
0x00 环境准备 XYHCMS官网:http://www.xyhcms.com/ 网站源码版本:XYHCMS V3.5(2017-12-04 更新) 程序源码下载:http://www.xyhc ...
- MySQL PARTITION 分区
MySQL HASH分区 http://www.cnblogs.com/chenmh/p/5644496.html RANGE分区:http://www.cnblogs.com/chenmh/p/56 ...
- NHibernate.3.0.Cookbook第一章第五节Setting up a base entity class
Setting up a base entity class设置一个实体类的基类 在这节中,我将给你展示怎么样去为我们的实体类设置一个通用的基类. 准备工作 完成前面三节的任务 如何去做 1.在Ent ...