本篇文章给大家分享的是有关SpringBoot中怎么利用Thymeleaf上传文件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。
创新互联专注于安阳企业网站建设,成都响应式网站建设公司,商城网站制作。安阳网站建设公司,为安阳等地区提供建站服务。全流程定制网站建设,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务
org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-thymeleaf
引入了 spring-boot-starter-thymeleaf 做页面模板引擎。
常用配置内容,单位支持 MB 或者 KB:
#支持的最大文件 spring.servlet.multipart.max-file-size=100MB #文件请求最大限制 spring.servlet.multipart.max-request-size=100MB
以上配置主要是通过设置 MultipartFile 的属性来控制上传限制,MultipartFile 是 Spring 上传文件的封装类,包含了文件的二进制流和文件属性等信息,在配置文件中也可对相关属性进行配置。
除过以上配置,常用的配置信息如下:
spring.servlet.multipart.enabled=true,是否支持 multipart 上传文件
spring.servlet.multipart.file-size-threshold=0,支持文件写入磁盘
spring.servlet.multipart.location=,上传文件的临时目录
spring.servlet.multipart.max-file-size=10Mb,最大支持文件大小
spring.servlet.multipart.max-request-sizee=10Mb,最大支持请求大小
spring.servlet.multipart.resolve-lazily=false,是否支持 multipart 上传文件时懒加载
@SpringBootApplication
public class FileUploadWebApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(FileUploadWebApplication.class, args);
}
//Tomcat large file upload connection reset
@Bean
public TomcatServletWebServerFactory tomcatEmbedded() {
TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
tomcat.addConnectorCustomizers((TomcatConnectorCustomizer) connector -> {
if ((connector.getProtocolHandler() instanceof AbstractHttp11Protocol>)) {
//-1 means unlimited
((AbstractHttp11Protocol>) connector.getProtocolHandler()).setMaxSwallowSize(-1);
}
});
return tomcat;
}
}TomcatServletWebServerFactory() 方法主要是为了解决上传文件大于 10M 出现连接重置的问题,此异常内容 GlobalException 也捕获不到。
上传页面:
Spring Boot file upload example
非常简单的一个 Post 请求,一个选择框选择文件、一个提交按钮,效果如下:
上传结果展示页面:
Spring Boot - Upload Status
效果图如下:

访问 localhost:8080 自动跳转到上传页面:
@GetMapping("/")
public String index() {
return "upload";
}上传业务处理:
@PostMapping("/upload")
public String singleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:uploadStatus";
}
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
// UPLOADED_FOLDER 文件本地存储地址
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded '" + file.getOriginalFilename() + "'");
} catch (IOException e) {
e.printStackTrace();
}
return "redirect:/uploadStatus";
}上面代码的意思就是,通过 MultipartFile 读取文件信息,如果文件为空跳转到结果页并给出提示;如果不为空读取文件流并写入到指定目录,最后将结果展示到页面。最常用的是最后两个配置内容,限制文件上传大小,上传时超过大小会抛出异常:

当然在真实的项目中我们可以在业务中会首先对文件大小进行判断,再将返回信息展示到页面。
这里演示的是 MultipartException 的异常处理,也可以稍微改造监控整个项目的异常问题。
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MultipartException.class)
public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {
redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
return "redirect:/uploadStatus";
}
}在项目中经常会有一次性上传多个文件的需求,我们稍作修改即可支持。
首先添加可以支持上传多文件的页面,内容如下:
Spring Boot files upload example
后端添加页面访问入口:
@GetMapping("/more")
public String uploadMore() {
return "uploadMore";
}在浏览器中输入网址,http://localhost:8080/more, 就会进入此页面。
MultipartFile 需要修改为按照数组的方式去接收。
@PostMapping("/uploadMore")
public String moreFileUpload(@RequestParam("file") MultipartFile[] files,
RedirectAttributes redirectAttributes) {
if (files.length==0) {
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:uploadStatus";
}
for(MultipartFile file:files){
try {
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
redirectAttributes.addFlashAttribute("message", "You successfully uploaded all");
return "redirect:/uploadStatus";
}同样是先判断数组是否为空,在循环遍历数组内容将文件写入到指定目录下。在浏览器中输入网址 http://localhost:8080/more, 选择三个文件进行测试,当页面出现以下信息时表示上传成功。
Spring Boot - Upload Status You successfully uploaded all
以上就是SpringBoot中怎么利用Thymeleaf上传文件,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。