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 ...
随机推荐
- Java知多少(42)泛型通配符和类型参数的范围
本节先讲解如何限制类型参数的范围,再讲解通配符(?). 类型参数的范围 在泛型中,如果不对类型参数加以限制,它就可以接受任意的数据类型,只要它是被定义过的.但是,很多时候我们只需要一部分数据类型就够了 ...
- R语言系列:生成数据
R语言系列:生成数据 (2014-05-04 17:41:57) 转载▼ 标签: r语言 教育 分类: 生物信息 生成规则数据1.使用“:“,如x=1:10,注意该方法既可以递增也可以递减,如y=10 ...
- Mysql系列七:分库分表技术难题之分布式全局唯一id解决方案
一.前言 在前面的文章Mysql系列四:数据库分库分表基础理论中,已经说过分库分表需要应对的技术难题有如下几个: 1. 分布式全局唯一id 2. 分片规则和策略 3. 跨分片技术问题 4. 跨分片事物 ...
- WebSphere集群环境修改IHS端口号的方法 分类: WebSphere 2015-08-06 13:41 14人阅读 评论(0) 收藏
参考资料:http://wenku.baidu.com/link?url=E9BkuEjJ16i9lg7l91L0-xhKCYkHV0mAnlwAeSlDCFM4TjZyk4ZVxmUu64BGd4F ...
- Linux 目录结构_004
前言 Linux文件系统层次标准,英文全称Filesystem Hierarchy Standard,英文简称FHS. 由于利用Linux来开发产品的团队和个人实在太多了,如果每个人都以自己的想法来配 ...
- java-信息安全(十一)-非对称加密算法ECC
概述 信息安全基本概念: ECC算法(Elliptic curve cryptography,椭圆曲线密码学) ECC 椭圆加密算法(ECC)是一种公钥加密体制,最初由Koblitz和Miller两人 ...
- mysql+redis
微博的系统架构,想用mysql+redis配合使用,具体操作步骤: 写入数据到Redis,,然后在写个运行cron的脚本,美妙读内存,并写入数据库即可. 使用注意: 1.MySQL使用需要注意的地方: ...
- Excel 保护工作表
1.选取整张表格,格式--设置单元格格式--锁定状态 2.将用户可编辑区域解锁 3.在审阅--保护工资表,设置除第一行不选,其他全选,添加密码保护,确定
- 在Ubuntu 14.04.1中安装VMware Tools的步骤
1. 在VMware Fusion 6.0.4下安装Ubuntu镜像:ubuntu-14.04.1-desktop-amd64.iso 2. 点击虚拟机菜单栏-安装VMware Tools 3. 在U ...
- OpenGL——折线图柱状图饼图绘制
折线图绘制代码: #include<iostream> //旧版本 固定管线 #include<Windows.h> #include <GL/glut.h> // ...