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 ...
随机推荐
- JVM 内部原理(四)— 基本概念之 JVM 结构
JVM 内部原理(四)- 基本概念之 JVM 结构 介绍 版本:Java SE 7 每位使用 Java 的程序员都知道 Java 字节码在 Java 运行时(JRE - Java Runtime En ...
- MSSQL 2012 修改所有表的架构Schame
ALTER SCHEMA [dbo] TRANSFER [sq_szswdjd].COM_Category ); declare csr1 cursor for select 'Name' = nam ...
- .net core实现跨域
什么是跨域在前面已经讲解过了,这里便不再讲解,直接上代码. 一.后台API接口 用.net core创建一个Web API项目负责给前端界面提供数据. 二.前端界面 建立两个MVC项目,模拟不同的ip ...
- Java知多少(79)哈希表及其应用
哈希表也称为散列表,是用来存储群体对象的集合类结构. 什么是哈希表 数组和向量都可以存储对象,但对象的存储位置是随机的,也就是说对象本身与其存储位置之间没有必然的联系.当要查找一个对象时,只能以某种顺 ...
- cannot open shared object file: No such file or directory
一般我们在Linux下执行某些外部程序的时候可能会提示找不到共享库的错误, 比如:error while loading shared libraries: libxxx.so: cannot ope ...
- classifier in maven
http://maven.apache.org/plugins/maven-deploy-plugin/examples/deploying-with-classifiers.html Beside ...
- linux dns 工具包 -- bind-utils
https://www.cnblogs.com/274914765qq/p/4817941.html
- [Laravel] 16 - DB: Eloquent
前言 一.大纲 写后端API,与数据库打交道无疑是很重要的角色. PHP数据库操作:从MySQL原生API到PDO PHP数据库操作:使用ORM Ref: [PHP] 07 - Json, XML a ...
- k8s(1)-使用kubeadm安装Kubernetes
安装前准备 1. 一台或多台主机,这里准备三台机器 角色 IP Hostname 配置(最低) 操作系统版本 主节点 192.168.0.10 master 2核2G CentOS7.6.1810 工 ...
- HandlerSocket介绍
HandlerSocket的原理 HandlerSocket的应用场景: MySQL自身的局限性,很多站点都采用了MySQL+Memcached的经典架构,甚至一些网站放弃MySQL而采用NoSQL产 ...