In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how to solve the problem of limited size of SpringBoot uploaded files". In daily operation, I believe many people have doubts about how to solve the problem of limited size of SpringBoot uploaded files. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the problem of "how to solve the problem of limited size of SpringBoot uploaded files." Next, please follow the editor to study!
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, it is because the uploaded file file exceeds the default configuration value of 1048576 bytes for spring. When uploading files, we usually use the MultipartFile API class to receive the files uploaded from the frontend. It can be seen that the default limit for MultipartFile files is 1048576 bytes, that is, 1m.
But in many cases, the ID photos or documents we take directly with our mobile phones are basically above 2m, which obviously can not meet our daily needs, so we have to modify the default configuration parameters.
2. Default parameters for uploading files
Different versions of SpringBoot have different 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 use the version of SpringBoot 2.1.3, and then directly set the parameter size in the configuration file:
# configure limited parameters spring: servlet: multipart: enabled: true # default support file upload max-file-size: 20MB # maximum support file size max-request-size: 30MB # maximum support request size # No limit parameter configuration spring: servlet: multipart: enabled: true # default support file upload max-file-size:-1 # No restrictions max-request-size:-1 # No restrictions
After setting up and restarting the project, you can upload the file successfully.
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. It is no longer necessary to write the configuration class separately and configure the parameters in the remote configuration center. This is so that the parameters can be dynamically modified according to temporary needs without restarting the project.
Common remote profile center services are Nacos, Apollo (Apollo), SpringCloud, and so on. I use the Nacos configuration center service:
Custom MultipartFileConfig configuration 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: Ishimo * @ date: 2022-03-23 19:18:51 * @ description: * / @ Configurationpublic class MultipartFileConfig {@ Value ("${config.multifile.maxFileSize}") private Long maxFileSize @ Value ("${config.multifile.maxRequestSize}") private Long maxRequestSize; @ Bean public MultipartConfigElement multipartConfigElement () {MultipartConfigFactory factory = new MultipartConfigFactory (); / * * single data size, * DataSize.ofMegabytes (maxFileSize) defaults to configuration bytes, converting bytes to MB * / factory.setMaxFileSize (DataSize.ofMegabytes (maxFileSize)) / / Total uploaded data size factory.setMaxRequestSize (DataSize.ofMegabytes (maxRequestSize)); return factory.createMultipartConfig ();}}
In this way, you can control the size of the uploaded file at any time!
At this point, the study on "how to solve the problem of limited file size uploaded by SpringBoot" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.