Springmvc File Upload--bean configuration "Org.springframework.web.multipart.commons.CommonsMultipartResolver"

Source: Internet
Author: User

First, Introduction

Spring MVC supports a common multi-channel upload parserCommonsMultipartResolver,在Spring的配置文件中对CommonsMultipartResolver Bean进行配置时,有一些可选的属性配置。

Second, analysis

After Baidu and view SPRINGMVC API documents have not found the relevant detailed configuration introduction, helpless can only view source code to look for clues:

In spring configuration file applicationcontext. After configuring the commonsmultipartresolver Bean in the XML , press CTRL + left mouse click Org.springframework.web.multipart.commons.CommonsMultipartResolver ' " into its source code interface, and in the source code is not found directly declared as" Maxuploadsize"is a property like this.

After careful review, in the source code, the comments on theCommonsmultipartresolver class See the relevant expression (labeled Red enlarged font):

1 /**2 * servlet-based {@linkMultipartresolver} implementation for3 * <a href= "Http://commons.apache.org/proper/commons-fileupload">apache Commons fileupload</a>4 * 1.2 or above.5  *6 * <p>provides "maxuploadsize", "Maxinmemorysize" and "defaultencoding" settings as 7 * Bean properties (inh Erited from {@link commonsfileuploadsupport}). See corresponding 8 * servletfileupload/diskfileitemfactory properties ("Sizemax", "Sizethreshold", 9 * "Headerencodin G ") for details in terms of defaults and accepted values.Ten  * One * <p>saves temporary files to the servlet container ' s temporary directory. A * Needs to being initialized <i>either</i> by an application context <i>or</i> - * via the constructor, takes a ServletContext (for standalone usage). -  * the  * @authorTrevor D. Cook -  * @authorJuergen Hoeller -  * @since29.09.2003 -  * @see#CommonsMultipartResolver (ServletContext) +  * @see#setResolveLazily -  * @seeOrg.springframework.web.portlet.multipart.CommonsPortletMultipartResolver +  * @seeOrg.apache.commons.fileupload.servlet.ServletFileUpload A  * @seeorg.apache.commons.fileupload.disk.DiskFileItemFactory at  */ -  Public classCommonsmultipartresolverextendsCommonsfileuploadsupport -         ImplementsMultipartresolver, Servletcontextaware { -...

As you can see, it contains the "maxuploadsize", "maxinmemorysize", "defaultencoding" Three properties that can be configured in the Bean:

    • maxuploadsize : Used to limit the maximum size of uploaded files, in bytes;
    • maxinmemorysize : The maximum number of bytes read from the file to memory (equivalent to cache), the default is 1024, in bytes;
    • defaultencoding : Represents the requested encoding format, which defaults to iso-8859-1.

  In addition , the source code can also be found to contain a declared property and the corresponding setter method:

  Property: resolvelazily

1     Private Boolean false;

  The corresponding setter method:

1     /**2 * Set Whether to resolve the multipart request lazily at the time3 * file or parameter access.4 * <p>default is "false", resolving the multipart elements immediately, throwing5 * Corresponding exceptions at the time of the {@link#resolveMultipart} call.6 * Switch this to ' true ' for lazy multipart parsing, throwing parse exceptions7 * Once the application attempts to obtain multipart files or parameters.8      */9      Public voidSetresolvelazily (Booleanresolvelazily) {Ten          This. resolvelazily =resolvelazily; One}

Therefore, it can be inferred that through spring's dependency injection function, the attribute can be configured and injected in the bean, and after a query, you know:

    • resolvelazily : Determine if you want to delay parsing the file. when resolvelazily is False (the default), the Parserequest () method is called immediately to parse the request data and then encapsulate the parsing result into defaultmultiparthttpservletrequest And when Resolvelazily is true, the request data is parsed in the Defaultmultiparthttpservletrequest Initializemultipart () method call Parserequest () method , and the Initializemultipart () method is called by the Getmultipartfiles () method, that is, the request data is parsed when the file information needs to be fetched, and this method uses lazy loading.

Iii. Examples

Configure a commonsmultipartresolver Bean (XML):

     <!--configuring file uploads using the parser -    <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver">        <!--specifies that the character set is Utf-8 -        < Propertyname= "Defaultencoding"value= "UTF-8"></ Property>                <!--Specify the maximum size of uploaded files -        < Propertyname= "Maxuploadsize"value= "10240"/>                <!--Specifies the size of the file loaded into memory -        < Propertyname= "Maxinmemorysize"value= "1024x768"/>                <!--Set delay parsing file -        < Propertyname= "Resolvelazily"value= "true"/>    </Bean>

Springmvc File Upload--bean configuration "Org.springframework.web.multipart.commons.CommonsMultipartResolver"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.