How to solve the problem of limited upload file size in SpringBoot
1. Console exception
org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateException: org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededException: The field file exceeds its maximum permitted size of 1048576 bytes. at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.handleParseFailure(StandardMultipartHttpServletRequest.java:121) at org.springframework.web.multipart.support.StandardMultipartHttpServletRequest.parseRequest(StandardMultipartHttpServletRequest.java:114)
As can be seen from the above exception, the reason is that the uploaded file exceeds the maximum value of spring's default configuration of 1048576 bytes. We usually use the MultipartFile interface class to upload files. When receiving files uploaded by the front end, it can be seen that the default limit for MultipartFile files is also 1048576 bytes, which is 1M.
But in many cases, the ID photos or documents we take directly with our mobile phones are basically above 2M. Obviously this cannot meet our daily needs, so we have to modify the default configuration parameter size. .
2. Default parameters for uploading files
Different versions of SpringBoot have different corresponding setting parameters:
Spring Boot 1.3.x and earlier
multipart.maxFileSize
multipart.maxRequestSize
Spring Boot 1.4.x and 1.5 .x
spring.http.multipart.maxFileSize
spring.http.multipart.maxRequestSize
Spring Boot 2.x
spring.servlet.multipart.maxFileSize
spring.servlet.multipart .maxRequestSize
3. Solution
3.1. Method 1 (modify parameters directly in the configuration file .yml or .properties)
For example, I The version of SpringBoot 2.1.3 is used, and then the parameter size is set directly in the configuration file:
#做限制的参数配置 spring: servlet: multipart: enabled: true #默认支持文件上传 max-file-size: 20MB # 最大支持文件大小 max-request-size: 30MB # 最大支持请求大小 #不做限制的参数配置 spring: servlet: multipart: enabled: true #默认支持文件上传 max-file-size: -1 #不做限制 max-request-size: -1 #不做限制
After setting, restart the project and the file can be uploaded successfully.
3.2. Method 2 (custom config configuration class)
Configure the parameters in the remote configuration file center. If it is in the configuration file in the configuration project, it is the same as method 1. There is no need to write a separate configuration class anymore. The parameters are configured in the remote configuration center so that the parameters can be dynamically modified according to temporary needs without restarting the project.
Common remote configuration file center services include Nacos, Apollo (Apollo), SpringCloud, etc. I use the Nacos configuration center service:
Custom MultipartFileConfigConfiguration class:
import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.MultipartConfigFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.util.unit.DataSize; import javax.servlet.MultipartConfigElement; /** * @author: 一宿君 * @date: 2022-03-23 19:18:51 * @description: */ @Configuration public class MultipartFileConfig { @Value("${config.multifile.maxFileSize}") private Long maxFileSize; @Value("${config.multifile.maxRequestSize}") private Long maxRequestSize; @Bean public MultipartConfigElement multipartConfigElement() { MultipartConfigFactory factory = new MultipartConfigFactory(); /** * 单个数据大小, * DataSize.ofMegabytes(maxFileSize)默认是配置字节,将字节转化为MB */ factory.setMaxFileSize(DataSize.ofMegabytes(maxFileSize)); // 总上传数据大小 factory.setMaxRequestSize(DataSize.ofMegabytes(maxRequestSize)); return factory.createMultipartConfig(); } }
So you can control the size of the uploaded file at any time!
The above is the detailed content of How to solve the problem of limited upload file size in SpringBoot. For more information, please follow other related articles on the PHP Chinese website!

Bytecodeachievesplatformindependencebybeingexecutedbyavirtualmachine(VM),allowingcodetorunonanyplatformwiththeappropriateVM.Forexample,JavabytecodecanrunonanydevicewithaJVM,enabling"writeonce,runanywhere"functionality.Whilebytecodeoffersenh

Java cannot achieve 100% platform independence, but its platform independence is implemented through JVM and bytecode to ensure that the code runs on different platforms. Specific implementations include: 1. Compilation into bytecode; 2. Interpretation and execution of JVM; 3. Consistency of the standard library. However, JVM implementation differences, operating system and hardware differences, and compatibility of third-party libraries may affect its platform independence.

Java realizes platform independence through "write once, run everywhere" and improves code maintainability: 1. High code reuse and reduces duplicate development; 2. Low maintenance cost, only one modification is required; 3. High team collaboration efficiency is high, convenient for knowledge sharing.

The main challenges facing creating a JVM on a new platform include hardware compatibility, operating system compatibility, and performance optimization. 1. Hardware compatibility: It is necessary to ensure that the JVM can correctly use the processor instruction set of the new platform, such as RISC-V. 2. Operating system compatibility: The JVM needs to correctly call the system API of the new platform, such as Linux. 3. Performance optimization: Performance testing and tuning are required, and the garbage collection strategy is adjusted to adapt to the memory characteristics of the new platform.

JavaFXeffectivelyaddressesplatforminconsistenciesinGUIdevelopmentbyusingaplatform-agnosticscenegraphandCSSstyling.1)Itabstractsplatformspecificsthroughascenegraph,ensuringconsistentrenderingacrossWindows,macOS,andLinux.2)CSSstylingallowsforfine-tunin

JVM works by converting Java code into machine code and managing resources. 1) Class loading: Load the .class file into memory. 2) Runtime data area: manage memory area. 3) Execution engine: interpret or compile execution bytecode. 4) Local method interface: interact with the operating system through JNI.

JVM enables Java to run across platforms. 1) JVM loads, validates and executes bytecode. 2) JVM's work includes class loading, bytecode verification, interpretation execution and memory management. 3) JVM supports advanced features such as dynamic class loading and reflection.

Java applications can run on different operating systems through the following steps: 1) Use File or Paths class to process file paths; 2) Set and obtain environment variables through System.getenv(); 3) Use Maven or Gradle to manage dependencies and test. Java's cross-platform capabilities rely on the JVM's abstraction layer, but still require manual handling of certain operating system-specific features.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1
Easy-to-use and free code editor

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
