温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringBoot中怎么利用Thymeleaf上传文件

发布时间:2021-06-15 14:01:35 来源:亿速云 阅读:265 作者:Leah 栏目:大数据

本篇文章给大家分享的是有关SpringBoot中怎么利用Thymeleaf上传文件,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。


  1. 添加依赖包

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

引入了 spring-boot-starter-thymeleaf 做页面模板引擎。

  1. 配置信息

常用配置内容,单位支持 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 上传文件时懒加载

  1. 启动类

@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 也捕获不到。 SpringBoot中怎么利用Thymeleaf上传文件

  1. 编写前端页面

  • 上传页面:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h2>Spring Boot file upload example</h2>
<form method="POST" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file" /><br/><br/>
    <input type="submit" value="Submit" />
</form>
</body>
</html>
  • 非常简单的一个 Post 请求,一个选择框选择文件、一个提交按钮,效果如下: SpringBoot中怎么利用Thymeleaf上传文件

  • 上传结果展示页面:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<h2>Spring Boot - Upload Status</h2>
<div th:if="${message}">
    <h3 th:text="${message}"/>
</div>
</body>
</html>
  • 效果图如下:

SpringBoot中怎么利用Thymeleaf上传文件

  1. 编写上传控制类

  • 访问 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 读取文件信息,如果文件为空跳转到结果页并给出提示;如果不为空读取文件流并写入到指定目录,最后将结果展示到页面。最常用的是最后两个配置内容,限制文件上传大小,上传时超过大小会抛出异常:

SpringBoot中怎么利用Thymeleaf上传文件

当然在真实的项目中我们可以在业务中会首先对文件大小进行判断,再将返回信息展示到页面。

  1. 异常处理

这里演示的是 MultipartException 的异常处理,也可以稍微改造监控整个项目的异常问题。

@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(MultipartException.class)
    public String handleError1(MultipartException e, RedirectAttributes redirectAttributes) {
        redirectAttributes.addFlashAttribute("message", e.getCause().getMessage());
        return "redirect:/uploadStatus";
    }
}

二、上传多个文件

在项目中经常会有一次性上传多个文件的需求,我们稍作修改即可支持。

  1. 前端页面

首先添加可以支持上传多文件的页面,内容如下:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h2>Spring Boot files upload example</h2>
<form method="POST" action="/uploadMore" enctype="multipart/form-data">
    文件1: <input type="file" name="file" /><br/><br/>
    文件2: <input type="file" name="file" /><br/><br/>
    文件3: <input type="file" name="file" /><br/><br/>
    <input type="submit" value="Submit" />
</form>
</body>
</html>
  1. 后台处理

后端添加页面访问入口:

@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上传文件,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI