Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to configure maxRequestSize for uploading files by springboot

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces the relevant knowledge of "how to configure the maxRequestSize of the uploaded file in springboot". The editor shows you the operation process through the actual case. The method of operation is simple, fast and practical. I hope that this article "how to configure the maxRequestSize of the uploaded file in springboot" can help you solve the problem.

Configure maxRequestSize for uploading files

The maxRequestSize of the uploaded file configured by springboot will vary according to the version of boot. When setting, it is quite crappy, and it will be set up in vain if you don't pay attention to it.

The configuration in application.properties is

Two methods of multipart.maxFileSize= 10Mbmultipart.maxRequestSize=100Mb setting upload File size before SpringBoot-2.0spring.servlet.multipart.max-file-size=10Mbspring.servlet.multipart.max-request-size=100MbSpringBoot-1.4spring.http.multipart.maxFileSize=10Mbspring.http.multipart.maxRequestSize=100MbSpringBoot-1.4 springboot 1.5.X Series processing

First, add the following code directly to the startup class of springboot.

Configure @ Bean in the configuration class. Note that @ Configuration is needed on the current configuration class.

/ * File upload configuration * @ return * / @ Bean public MultipartConfigElement multipartConfigElement () {MultipartConfigFactory factory = new MultipartConfigFactory (); / / maximum file factory.setMaxFileSize ("10240KB"); / / KB,MB / set the total upload data size factory.setMaxRequestSize ("102400KB"); return factory.createMultipartConfig () }

Second, add the following configuration information to application.properties

Configuration file is configured directly, which is consistent with the springboot2.x version

Application.properties

Spring.servlet.multipart.max-file-size=20MB spring.servlet.multipart.max-request-size=20MB

Be careful

Spring.http.multipart.maxFileSize=10Mb

Spring.http.multipart.maxRequestSize=10Mb

Has expired >

Application.yml

Spring: servlet: multipart: max-file-size: 20MB max-request-size: 20MB

MaxFileSize is a single file size

MaxRequestSize is to set the total uploaded data size.

That's all right.

Define it according to your own needs. It can only be MB and KB. The letters are uppercase and lowercase, and the Long type is OK.

In addition, this is the transformation in the MultipartConfigFactory class, you can take a look at it.

Private long parseSize (String size) {Assert.hasLength (size, "Size must not be empty"); size = size.toUpperCase (Locale.ENGLISH); if (size.endsWith ("KB")) {return Long.valueOf (size.substring (0, size.length ()-2)) * 1024L;} else {return size.endsWith ("MB")? Long.valueOf (size.substring (0, size.length ()-2)) * 1024L * 1024L: Long.valueOf (size);}} Springboot 2.x Series configuration

First kind

Add a configuration to the configuration file

@ Bean public MultipartConfigElement multipartConfigElement () {MultipartConfigFactory factory = new MultipartConfigFactory (); / / the maximum file size is 10m factory.setMaxRequestSize; / / the total upload data size is 10m factory.setMaxRequestSize (DataSize.of (10, DataUnit.MEGABYTES)); return factory.createMultipartConfig ();}

The second kind

Configuration file direct configuration

Application.properties

Spring.servlet.multipart.max-file-size=20MB spring.servlet.multipart.max-request-size=20MB

Application.yml

This is the end of spring: servlet: multipart: max-file-size: 20MB max-request-size: 20MB on "how to configure maxRequestSize for uploaded files in springboot". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report