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> < ...
随机推荐
- ActiveMQ持久化机制
用户注册成功后发短信提醒 同步http 异步mq JMS中两种通讯模式: 发布订阅 一对多 topic 去过消费者集群的话 都会消费 消息队列 点对点 queue 去过消费者集群的话 ...
- tkinter之对话框
对话框的一个例子: from tkinter.dialog import * from tkinter import * def investigation(): d=Dialog(None,titl ...
- python类初探
class human: is_alive=True is_man=True def __init__(self,age): print('this is __init__() method, whi ...
- BZOJ 1673 [Usaco2005 Dec]Scales 天平:dfs 启发式搜索 A*搜索
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1673 题意: 有n个砝码(n <= 1000),重量为w[i]. 你要从中选择一些砝 ...
- BZOJ 1202 [HNOI2005]狡猾的商人:并查集(维护距离)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1202 题意: 有一个账本,记录了n个月的盈亏. 每个月的数值:正为盈,负为亏. 你知道m个 ...
- 分享知识-快乐自己:初始 Struts2 (基本概念)及 搭建第一个Demo
1):struts2 的基本概念: 1-1):Struts2 是什么? 1.Struts2是一个基于MVC设计模式的Web应用框架,它本质上相当于一个servlet,在MVC设计模式中,Struts2 ...
- /boot下面文件说明
config-3.10.0-229.el7.x86_64 <==此版本核心被编译时选择的功能与模组设定档 grub/ <==旧版 grub1 ,不需要理会这目录了! grub2/ < ...
- SimpliciTI简介
SimpliciTI简介 SimpliciTI是TI开发的一份专门针对其CCxxxx系列无线通信芯片的网络协议.按照其官方说法SimpliciTI是一个基于连接的点对点通讯协议.它支持两种网络拓扑结构 ...
- hive 面试题
使用 Hive或者自定义 MR 实现如下逻辑 product_no lac_id moment start_time user_id county_id staytime city_id 134291 ...
- 机器学习、图像识别方面 书籍推荐 via zhihu
机器学习.图像识别方面 书籍推荐 作者:小涛 链接:https://www.zhihu.com/question/20523667/answer/97384340 来源:知乎 著作权归作者所有.商业转 ...