1. Brief description
Data in multipart format splits a form into multiple parts, each part corresponding to an input field. In the general form input field, text-type data will be placed in the corresponding part, but if the file is uploaded, its corresponding part can be binary. Similar to this:
2. Configure the multipart parser
Although multipart requests may seem complicated, it is easy to handle them in Spring MVC. Before writing a controller method to handle file upload, we must configure a multipart parser to tell the DispatcherServlet how to read the multipart request.
Spring has two built-in implementations of MultipartResolver:
Configuration of StandardServletMultipartResolver:
1. Statement Bean:
Configuration in applicationContext.xml
The code copy is as follows:<bean id="multipartResolver"brush:java;"> @Bean(name = "multipartResolver") public StandardServletMultipartResolver getStandardServletMultipartResolver(){ return new StandardServletMultipartResolver(); }
tips: The name of the multipart parser must be multipartResolver, otherwise an error will be reported.
2. Configure upload parameters:
* web.xml configuration
<servlet> <servlet-name>dispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <multipart-config> <!--Upload to /tmp/upload directory--> <location>/tmp/upload</location> <!--File size is 2M--> <max-file-size>2097152</max-file-size> <!--The entire request does not exceed 4M--> <max-request-size>4194304</max-request-size> <!--All files must be written to disk--> <file-size-threshold>0</file-size-threshold> </multipart-config> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping>
* Configuration in the configuration class
Inherited in the configuration class of AbstractAnnotationConfigDispatcherServletInitializer
@Override protected void customizeRegistration(ServletRegistration.Dynamic registration) { //Upload to the /tmp/upload directory, the file size is 2M, the entire request does not exceed 4M, and all files must be written to disk registration.setMultipartConfig(new MultipartConfigElement("E://upload_ftp",2097152,4194304,0)); }CommonsMultipartResolver configuration:
1. Declare beans and configure upload parameters
<bean id="multipartResolver"> <!--Set the upload directory/tmp/upload; the maximum file capacity is set to 2M; the maximum memory size is set to 0, which means that all files will be written to disk; the overall maximum capacity of multipart request cannot be set--> <property name="uploadTempDir" value="/tmp/upload"/> <property name="maxUploadSize" value="2097152"/> <property name="maxInMemorySize" value="0"/> </bean>
the difference:
1. CommonsMultipartResolver Compared with StandardServletMultipartResolver, it is impossible to set the overall maximum capacity of multipart requests.
2. CommonsMultipartResolver does not force setting a temporary file path. By default, this path is the temporary directory of the Servlet container. StandardServletMultipartResolver must set the temporary file path to execute normally. (The uploaded directories mentioned above are all temporary file paths)
3. SpringMVC handles requests
1. Front-end Form form
<form action="/picture" method="post" enctype="multipart/form-data"> <input type="file" name="picture"> <input type="submit"> </form>
tips: Enctype="multipart/form-data" needs to be set to tell SpringMVC that this is a Multipart request.
2. Backend MVC accepts requests
@RequestMapping(value = "/picture",method = RequestMethod.POST) public String getHome(@RequestPart("picture") MultipartFile picture) throws IOException { String name = picture.getName(); byte[] bytes = picture.getBytes(); picture.transferTo(new File("/"+picture.getOriginalFilename())); // When saving to the file system here, use a relative path, for example, the configuration here is /. Based on the configured upload directory. That is, the file path E:/upload_ftp/ is the saved directory return "home"; }tips: 1. @RequestPart("picture"): When the registration form is submitted, the p essence attribute will give an array of bytes, which contains the corresponding part data in the request (specified through @RequestPart). If the user submits the form without selecting a file, the array will be empty (rather than null). So we can even use the byte[] array to receive Multipart requests instead of MultipartFile.
2. MultipartFile: Using the MultipartFile method to receive provides us with many methods to carry out the next work...
3. Accept uploaded files in the form of a Part
As for the subject, there is not much difference between the Part interface and the MultipartFile. In many cases, the name of the Part method is exactly the same as the name of the MultipartFile method. There are some similarities, but slightly different, such as getSubmittedFileName() corresponding to getOriginalFilename(). Similarly, write() corresponds to transferTo() , and with the help of this method we can write the uploaded file to the file system.
It is worth mentioning that if you accept file uploads through Part parameters when writing controller methods, then there is no need to set up MultipartResolver. Only when using MultipartFile, we need MultipartResolver.
@RequestMapping(value = "/picture",method = RequestMethod.POST) public String getHome(@RequestPart("picture") Part picture) throws IOException { picture.write("/"+picture.getSubmittedFileName()); return "home"; }The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.