The happiest thing in the world is to strive for the ideal. --Socrates
1. Multipart Request
The result of the request formed by the general form submission is very simple, that is, the "&" character is divided into multiple name-value pairs, such as: "Firstname=charles&lastname=bob". This form of encoding is sufficient for typical text-based form submissions, but it is not enough to transmit binary data in a multipart request.
For multipart requests, binary data in multipart format splits a form into multiple parts (part), one for each input field. Text-based data is placed in the section of the normal form input field, but if you upload the file, it can be binary.
2. Configuring the multipart Parser
Starting with Spring 3.1, Spring has built in two implementations of the Multipartresolver class to choose from:
1) Commonmultipartresolver: Use Jakarta Commons FileUpload to parse the multipart request.
2) Standardservletmultipartresolver: dependent on Servlet 3.0 support for multipart requests.
2.1 Resolving multipart requests using Servlet 3.0 (configuration Standardservletmultipartresolver)
PackageCom.spring.fileupload.config;Importjavax.servlet.MultipartConfigElement;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRegistration.Dynamic;ImportOrg.springframework.web.WebApplicationInitializer;ImportOrg.springframework.web.servlet.DispatcherServlet; Public classFileuploadwebappinitialzerImplementswebapplicationinitializer{ Public voidOnstartup (ServletContext ServletContext)throwsservletexception {dispatcherservlet ds=NewDispatcherservlet (); Dynamic Registration= Servletcontext.addservlet ("Appservlet", DS); Registration.addmapping ("/"); Registration.setmultipartconfig (NewMultipartconfigelement ("/temp/uploads")); }}
2) If we configure the Dispatcherservelt Servlet initialization class to inherit the Abstractannotationconfigdispatcherservletinitilaizer or
Abstractdispatcherservletinitializer, we can configure the specifics of the multipart by overloading the Customizeregistration () method.
PackageCom.spring.fileupload.config;Importjavax.servlet.MultipartConfigElement;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException;Importjavax.servlet.ServletRegistration.Dynamic;ImportOrg.springframework.web.WebApplicationInitializer;ImportOrg.springframework.web.servlet.DispatcherServlet;ImportOrg.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; Public classFileuploadwebappinitialzerextendsAbstractannotationconfigdispatcherservletinitializer {@OverrideprotectedClass<?>[] getrootconfigclasses () {//TODO auto-generated Method Stub return NewClass<?>[] {rootconfig.class }; } @OverrideprotectedClass<?>[] getservletconfigclasses () {//TODO auto-generated Method Stub return NewClass<?>[] {webconfig.class }; } @Overrideprotectedstring[] Getservletmappings () {//TODO auto-generated Method Stub return NewString[] {"/" }; } @Overrideprotected voidcustomizeregistration (Dynamic registration) {//TODO auto-generated Method Stub NewMultipartconfigelement ("/temp/uploads"); }}
3) In addition to specifying a temporary directory for uploading files, we can also configure the specifics of multipart with other constructors, such as:
(1) Limit the maximum capacity of uploaded files (in bytes), the default is unlimited.
(2) Limit the maximum capacity (in bytes) of the entire multipart request, which is not limited by default.
(3) in the process of uploading, if the file reaches the specified size (in bytes), it will be written to the temporary file path, which defaults to 0.
2.2 Configuring the Jakarta Commons FileUpload multipart parser
PackageCom.spring.fileupload.config;Importjava.io.IOException;ImportOrg.springframework.context.annotation.Bean;ImportOrg.springframework.context.annotation.ComponentScan;Importorg.springframework.context.annotation.Configuration;ImportOrg.springframework.core.io.FileSystemResource;ImportOrg.springframework.web.multipart.MultipartResolver;ImportOrg.springframework.web.multipart.commons.CommonsMultipartResolver;ImportORG.SPRINGFRAMEWORK.WEB.SERVLET.CONFIG.ANNOTATION.ENABLEWEBMVC;Importorg.springframework.web.servlet.config.annotation.webmvcconfigureradapter;@ Configuration@enablewebmvc@componentscan ("Com.spring.fileupload") Public classWebconfigextendswebmvcconfigureradapter{@Bean PublicMultipartresolver Multipartresolver ()throwsIOException {commonsmultipartresolver multipartresolver=NewCommonsmultipartresolver (); //Set the temp directory pathMultipartresolver.setuploadtempdir (NewFilesystemresource ("/temp/uploads")); //limit the maximum file size to 2MBMultipartresolver.setmaxuploadsize (2097152); //limit the maximum memory size to 0 bytesMultipartresolver.setmaxinmemorysize (0); returnMultipartresolver; } }
3, processing multipart request
<!--the Enctype property setting tells the browser to submit the form as multipart data - <formAction= "/uploadimg"enctype= "Multipart/form-data"> <label>Upload picture</label> <!--the Accept property setting restricts the upload file type to jpeg,png and GIF pictures - <inputtype= "File"name= "Picture"Accept= "Image/jpeg,image/png,image/gif"> </form>
When a form is submitted, the Controller method we write will accept the uploaded file data.
@RequestMapping (value= "/uploadimg") public String uploadpicture ( @RequestPart ( byte[] picture ) { return "Success"; }
Spring_springmvc processing file uploads