十三:SpringBoot-基于Yml配置方式,实现文件上传逻辑
SpringBoot-基于Yml配置方式,实现文件上传逻辑
1、文件上传
文件上传是项目开发中一个很常用的功能,常见的如头像上传,各类文档数据上传等。SpringBoot使用MultiPartFile接收来自表单的file文件,然后执行上传文件。该案例基于SpringBoot2.0中yml配置,管理文件上传的常见属性。该案例演示单文件上传和多文件上传。
2、搭建文件上传界面
2.1 引入页面模板Jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.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/>
2.3 配置页面入口
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class PageController {
/**
* 上传页面
*/
@GetMapping("/uploadPage")
public String uploadPage (){
return "upload" ;
}
}
3、SpringBoot整合上传文件
3.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
3.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" ;
}
}
}
十三:SpringBoot-基于Yml配置方式,实现文件上传逻辑的更多相关文章
- SpringBoot2.0 基础案例(14):基于Yml配置方式,实现文件上传逻辑
本文源码 GitHub地址:知了一笑 https://github.com/cicadasmile/spring-boot-base 一.文件上传 文件上传是项目开发中一个很常用的功能,常见的如头像上 ...
- 基于 Django的Ajax实现 文件上传
---------------------------------------------------------------遇到困难的时候,勇敢一点,找同学朋友帮忙,找导师求助. Ajax Ajax ...
- Resumable.js – 基于 HTML5 File API 的文件上传
Resumable.js 是一个 JavaScript 库,通过 HTML5 文件 API 提供,稳定和可恢复的批量上传功能.在上传大文件的时候通过每个文件分割成小块,每块在上传失败的时候,上传会不断 ...
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile(转)
文件上传项目的源码下载地址:http://download.csdn.net/detail/swingpyzf/6979915 一.配置文件:SpringMVC 用的是 的MultipartFil ...
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile
一.配置文件:SpringMVC 用的是 的MultipartFile来进行文件上传 所以我们首先要配置MultipartResolver:用于处理表单中的file <!-- 配置Multipa ...
- SpringBoot集成基于tobato的fastdfs-client实现文件上传下载和删除
1. 简介 基于tobato的fastdfs-client是一个功能完善的FastDFS客户端工具,它是在FastDFS作者YuQing发布的客户端基础上进行了大量的重构,提供了上传.下载.删除. ...
- SpringBoot之GZip压缩,HTTP/2,文件上传,缓存配置
1 设置应用端口以及context # HTTP Server port server.port=8080 # Make the application accessible on the given ...
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2 spring-mv ...
- springboot 静态资源访问,和文件上传 ,以及路径问题
springboot 静态资源访问: 这是springboot 默认的静态资源访问路径 访问顺序依次从前到后(http://localhost:8080/bb.jpg) spring.resourc ...
随机推荐
- 一个简单的springboot+mybatis-plus+thymeleaf的学生管理系统
一.登录功能 1.1登录所涉及的功能主要包括拦截器,过滤器,用户在未登录的时候,访问页面会阻止访问的,如图所示: 实现这个功能的主要代码如下所示 1 //拦截器 2 public class Logi ...
- 被自己以为的GZIP秀到了
问题的开始 我司某产品线有这么一个神奇接口 (https://host/path/customQuery) 该接口在预发或线上缓存正常的情况下TTFB为150ms左右(可以认为服务处理时间差不多就是T ...
- 第三章节 BJROBOT 角速度校正 【ROS全开源阿克曼转向智能网联无人驾驶车】
1.把小车平放在地板上,用资料里的虚拟机,打开一个终端 ssh 过去主控端启动roslaunch znjrobot bringup.launch . 2.再打开一个终端 ssh 过去主控端,启动校 ...
- Linux下nginx反向代理负载均衡几种方式以及配置
下面以ip地址192.168.1.1 和192.168.1.2举例 1.轮询 (1).轮询:每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除. upstream ...
- 日常入坑1-Calendar类
1.当前时间是一号的时候,通过计算上一天的日期的时候,需要注意了 Calendar calendar = Calendar.getInstance();calendar.set(2019,10,1); ...
- haproxy 支持 websocket
haproxy支持websocket feat 通过嗅探http请求中的Connection: Upgrade Upgrade: websocket头部,来自动识别是否是websocket连接,识别成 ...
- Rejecting mapping update to [xxx] as the final mapping would have more than 1 type: [xxx, xx]
说明: 1.elasticsearch 版本 6.3.1 2.在同一个index下创建两个type时报错,信息如下: 在创建第二个type:solr时,先前已经在相同索引下创建了一个type:es [ ...
- Approach for Unsupervised Bug Report Summarization 无监督bug报告汇总方法
AUSUM: approach for unsupervised bug report summarization 1. Abstract 解决的bug被归类以便未来参考 缺点是还是需要手动的去细读很 ...
- 你必须要懂的 Github 开源协议
作为一个开源社区的活跃者,那些开源协议你都懂什么意思吗? 列两个: Apache License 可以: 商用.修改.分发 但是要声明作者来源和你的修改以及协议 MIT License 只要声明版权 ...
- JavaFX之班级未交作业统计
前言 最近转移了系统平台,用上了Ubuntu1804版本系统,原来用C#写的Windows窗体软件也不能用了,而且自己在班级上每周都需要收作业,所以写了这个软件.这篇博客主要记录这个JavaFX应用的 ...