Spring Boot tutorial (four): file upload

  File upload is a relatively common and well-known functional module by users. Common scenarios include avatar settings, product previews, report file saving, etc. In these scenarios, the file upload function needs to be used. This article will upload files The general process and functional design are introduced in detail, and combined with practical cases to explain how to use Spring Boot to implement file upload and related precautions, the entire process of file upload is closed-loop.

  Due to the existence of Spring Boot's automatic configuration mechanism, we do not need to make redundant settings. As long as the web starter module has been introduced in the pom file, the file upload function can be directly carried out. In the previous article, we have integrated the web module into the project. So there is no need for integration. Although you can use file upload without configuration, some developers may have some special requirements when uploading files. Therefore, it is also necessary to introduce the common settings of MultipartFile in Spring Boot. The configuration and default values ​​are as follows:

Insert picture description here

spring.servlet.multipart.enabled 是否支持 multipart 上传文件,默认支持

spring.servlet.multipart.file-size-threshold 文件大小阈值,当大于这个阈值时将写入到磁盘,否则存在内存中,(默认值 0 ,一般情况下不用特意修改)

spring.servlet.multipart.location 上传文件的临时目录

spring.servlet.multipart.max-file-size 最大支持文件大小,默认 1 M ,该值可适当的调整

spring.servlet.multipart.max-request-size 最大支持请求大小,默认 10 M

spring.servlet.multipart.resolve-lazily判断是否要延迟解析文件(相当于懒加载,一般情况下不用特意修改),默认 false

  For details on how to operate, please refer to: link

Guess you like

Origin blog.csdn.net/Tracycoder/article/details/113919392