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 ...
随机推荐
- TCP/IP模型及OSI七层参考模型各层的功能和主要协议
注:网络体系结构是分层的体系结构,学术派标准OSI参考模型有七层,而工业标准TCP/IP模型有四层.后者成为了事实上的标准,在介绍时通常分为5层来叙述但应注意TCP/IP模型实际上只有四层. 1.TC ...
- @Transactional(rollbackFor = Exception.class)
@Transactional(rollbackFor = Exception.class)这个注解只有在出异常时才会回滚,需要回滚时没有异常也要人为制造异常(自定义异常)所以,如果使用了异常捕获,很有 ...
- Java之CountDownLatch使用
CountDownLatch,一个同步辅助类,在完成一组正在其他线程中执行的操作之前,它允许一个或多个线程一直等待. 主要方法 public CountDownLatch(int count); pu ...
- [Bayes] Understanding Bayes: A Look at the Likelihood
From: https://alexanderetz.com/2015/04/15/understanding-bayes-a-look-at-the-likelihood/ Reading note ...
- [Bayes] Understanding Bayes: Visualization of the Bayes Factor
From: https://alexanderetz.com/2015/08/09/understanding-bayes-visualization-of-bf/ Nearly被贝叶斯因子搞死,找篇 ...
- [TF] Architecture - Computational Graphs
阅读笔记: 仅希望对底层有一定必要的感性认识,包括一些基本核心概念. Here只关注Graph相关,因为对编程有益. TF – Kernels模块部分参见:https://mp.weixin.qq.c ...
- Java -- 异常的捕获及处理 -- 异常类的继承结构
7.1.3 异常类的继承结构 在整个Java的异常结构中,实际上有两个最常用的类,分别为Exception和Error,这两个类全都是Throwable的子类. ⊙ Exception : 一般标识的 ...
- Mac OSX安装启动 zookeeper
安装 zookeeper支持brew安装 ➜ ~ brew info zookeeper zookeeper: stable (bottled), HEAD Centralized server fo ...
- 【代码审计】大米CMS_V5.5.3 代码执行漏洞分析
0x00 环境准备 大米CMS官网:http://www.damicms.com 网站源码版本:大米CMS_V5.5.3试用版(更新时间:2017-04-15) 程序源码下载:http://www ...
- [C] 在 C 语言编程中实现动态数组对象
对于习惯使用高级语言编程的人来说,使用 C 语言编程最头痛的问题之一就是在使用数组需要事先确定数组长度. C 语言本身不提供动态数组这种数据结构,本文将演示如何在 C 语言编程中实现一种对象来作为动态 ...