SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
本文源码:GitHub·点这里 || GitEE·点这里
一、文件上传
文件上传是项目开发中一个很常用的功能,常见的如头像上传,各类文档数据上传等。SpringBoot使用MultiPartFile接收来自表单的file文件,然后执行上传文件。该案例基于SpringBoot2.0中yml配置,管理文件上传的常见属性。该案例演示单文件上传和多文件上传。
二、搭建文件上传界面
1、引入页面模板Jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、编写简单的上传页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<hr/>
<h3>1、单文件上传</h3>
<form method="POST" action="/upload1" enctype="multipart/form-data">
上传人:<input type="text" name="userName" /><br/>
文件一:<input type="file" name="file" /><br/>
<input type="submit" value="Submit" />
</form>
<hr/>
<h3>2、多文件上传</h3>
<form method="POST" action="/upload2" enctype="multipart/form-data">
上传人:<input type="text" name="userName" /><br/>
文件一:<input type="file" name="file" /><br/>
文件二:<input type="file" name="file" /><br/><br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
<hr/>
3、配置页面入口
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class PageController {
/**
* 上传页面
*/
@GetMapping("/uploadPage")
public String uploadPage (){
return "upload" ;
}
}
三、与SpringBoot2.0整合
1、核心配置文件
上传文件单个限制
max-file-size: 5MB
上传文件总大小限制
max-request-size: 6MB
spring:
application:
# 应用名称
name: node14-boot-file
servlet:
multipart:
# 启用
enabled: true
# 上传文件单个限制
max-file-size: 5MB
# 总限制
max-request-size: 6MB
2、文件上传核心代码
如果单个文件大小超出1MB,抛出异常
FileSizeLimitExceededException:
如果上传文件总大小超过6MB,抛出异常
SizeLimitExceededException:
这样就完全验证了YML文件中的配置,有效且正确。
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Map;
@RestController
public class FileController {
private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class) ;
/**
* 测试单个文件上传
*/
@RequestMapping("/upload1")
public String upload1 (HttpServletRequest request, @RequestParam("file") MultipartFile file){
Map<String, String[]> paramMap = request.getParameterMap() ;
if (!paramMap.isEmpty()){
LOGGER.info("paramMap == >>{}",paramMap);
}
try{
if (!file.isEmpty()){
// 打印文件基础信息
LOGGER.info("Name == >>{}",file.getName());
LOGGER.info("OriginalFilename == >>{}",file.getOriginalFilename());
LOGGER.info("ContentType == >>{}",file.getContentType());
LOGGER.info("Size == >>{}",file.getSize());
// 文件输出地址
String filePath = "F:/boot-file/" ;
new File(filePath).mkdirs();
File writeFile = new File(filePath, file.getOriginalFilename());
file.transferTo(writeFile);
}
return "success" ;
} catch (Exception e){
e.printStackTrace();
return "系统异常" ;
}
}
/**
* 测试多文件上传
*/
@RequestMapping("/upload2")
public String upload2 (HttpServletRequest request, @RequestParam("file") MultipartFile[] fileList){
Map<String, String[]> paramMap = request.getParameterMap() ;
if (!paramMap.isEmpty()){
LOGGER.info("paramMap == >>{}",paramMap);
}
try{
if (fileList.length > 0){
for (MultipartFile file:fileList){
// 打印文件基础信息
LOGGER.info("Name == >>{}",file.getName());
LOGGER.info("OriginalFilename == >>{}",file.getOriginalFilename());
LOGGER.info("ContentType == >>{}",file.getContentType());
LOGGER.info("Size == >>{}",file.getSize());
// 文件输出地址
String filePath = "F:/boot-file/" ;
new File(filePath).mkdirs();
File writeFile = new File(filePath, file.getOriginalFilename());
file.transferTo(writeFile);
}
}
return "success" ;
} catch (Exception e){
return "fail" ;
}
}
}
四、源代码地址
GitHub·地址
https://github.com/cicadasmile/spring-boot-base
GitEE·地址
https://gitee.com/cicadasmile/spring-boot-base

SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑的更多相关文章
- 十三:SpringBoot-基于Yml配置方式,实现文件上传逻辑
SpringBoot-基于Yml配置方式,实现文件上传逻辑 1.文件上传 2.搭建文件上传界面 2.1 引入页面模板Jar包 2.2 编写简单的上传页面 2.3 配置页面入口 3.SpringBoot ...
- SpringBoot2.0 基础案例(12):基于转账案例,演示事务管理操作
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.事务管理简介 1.事务基本概念 一组业务操作ABCD,要么全部 ...
- 基于 Django的Ajax实现 文件上传
---------------------------------------------------------------遇到困难的时候,勇敢一点,找同学朋友帮忙,找导师求助. Ajax Ajax ...
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile(转)
文件上传项目的源码下载地址:http://download.csdn.net/detail/swingpyzf/6979915 一.配置文件:SpringMVC 用的是 的MultipartFil ...
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置MultipartResolver:用于处理表单中的file <!-- 配置Multipa ...
- Resumable.js – 基于 HTML5 File API 的文件上传
Resumable.js 是一个 JavaScript 库,通过 HTML5 文件 API 提供,稳定和可恢复的批量上传功能.在上传大文件的时候通过每个文件分割成小块,每块在上传失败的时候,上传会不断 ...
- SpringBoot集成基于tobato的fastdfs-client实现文件上传下载和删除
1. 简介 基于tobato的fastdfs-client是一个功能完善的FastDFS客户端工具,它是在FastDFS作者YuQing发布的客户端基础上进行了大量的重构,提供了上传.下载.删除. ...
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2 spring-mv ...
- 基于jquery ajax的多文件上传进度条
效果图 前端代码,基于jquery <!DOCTYPE html> <html> <head> <title>主页</title> < ...
随机推荐
- C# 序列化反序列化 list<>
public class XMLTest { [Test] public static void Test() { Name1 name = new Name1(); name.Id = " ...
- BZOJ 3410 [Usaco2009 Dec]Selfish Grazing 自私的食草者:贪心【最多线段覆盖】
题目链接:http://begin.lydsy.com/JudgeOnline/problem.php?id=1324 题意: 给你n个区间,问你最多能选择多少个区间使得它们不相互覆盖. 题解: RQ ...
- DLL进一步讲解:extern "C" __declspec(dllexport)
一.__declspec(dllexport): 将一个函数声名为导出函数,就是说这个函数要被其他程序调用,即作为DLL的一个对外函数接口. 通常它和extern "C" ...
- 作业2nd
1. 国内: 雷军作为中国互联网代表人物及环球年度电子商务创新首领人物,曾获中国经济年度人物及十大财智首领人物.中国互联网年度人物等多项国表里荣誉,并当选<福布斯>(亚洲版)2014年度贸 ...
- kettle导数删除并插入更新数据_20161130
这里有3个表 仅是时间维度不同 天 周 月,现在需要把昨天数据每天添加进入这3个表 由于业务上会有退货等情况,因此需要先把这些表原来的部分数据删除 再从那个时间点进行更新. 天需要先删除前7天的数据, ...
- hdu3518 Boring Counting[后缀排序]
裸的统计不同的重复出现子串(不重叠)种数的题.多次使用后缀排序要注意小细节.y数组在重复使用时一定要清空,看那个line25 +k就明白了 ,cnt也要清空,为什么就不说了 #include<b ...
- 【Lintcode】033.N-Queens
题目: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two que ...
- poj3254二进制放牛——状态压缩DP
题目:http://poj.org/problem?id=3254 利用二进制压缩状态,每一个整数代表一行的01情况: 注意预处理出二进制表示下没有两个1相邻的数的方法,我的方法(不知为何)错了,看到 ...
- TFS 备注
1,更改任何文件, 先checkout, 再继续更改. 2. 更新sln时, 一定要更新include文件 3. 每次提交代码放到shelf上, 自己本地建立2个workspace, 来进行coder ...
- 网络工具 NetCat
http://netcat.sourceforge.net/ windows 版本 https://joncraton.org/blog/46/netcat-for-windows/ https:// ...