SpringMVC file upload multi file upload instance

  • 2020-04-01 02:48:04
  • OfStack

The DispatcherServlet must be explicitly told how to handle MultipartRequest.
SpringMVC provides the following ways to upload files
Configure xxx-servlet.xml and add the following code:


<bean id="multipartResolver"  
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">  
        <!--  Set the maximum size of the uploaded file to 1MB -->  
    <property name="maxUploadSize">  
        <value>1048576</value>  
    </property>  
</bean>  

The same code at the page code block index 0

Notice that the file size here is actually just the total size of all the files
If you configure the file size then you need to configure the exception information control
So you need to configure the exception display


<!-- SpringMVC When the upload file limit is exceeded, it is thrown org.springframework.web.multipart.MaxUploadSizeExceededException -->  
    <!--  The exception is SpringMVC Thrown while checking uploaded file information, and not yet entered Controller In the method  -->  
    <bean id="exceptionResolver"  
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>  
                <!--  encounter MaxUploadSizeExceededException When an exception occurs, it automatically jumps to /WEB-INF/jsp/error_fileupload.jsp page  -->  
                <prop  
                    key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>  
            </props>  
        </property>  
    </bean>


<!-- SpringMVC When the upload file limit is exceeded, it is thrown org.springframework.web.multipart.MaxUploadSizeExceededException -->  
    <!--  The exception is SpringMVC Thrown while checking uploaded file information, and not yet entered Controller In the method  -->  
    <bean id="exceptionResolver"  
        class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">  
        <property name="exceptionMappings">  
            <props>  
                <!--  encounter MaxUploadSizeExceededException When an exception occurs, it automatically jumps to /WEB-INF/jsp/error_fileupload.jsp page  -->  
                <prop  
                    key="org.springframework.web.multipart.MaxUploadSizeExceededException">error_fileupload</prop>  
            </props>  
        </property>  
    </bean>  

Or lazy point, do not set the size, then the final default is the value is not limited. If you must control the return information, consider returning data in the specified format in the local exception handling, such as JSON

After configuring the page, as always, we need to add: enctype="multipart/form-data" in the form form.
Then there are the actions that need to be processed, which can be done in one of two ways
The first way:


public String login( @RequestParam MultipartFile file, Model model) {   
 ...........................    
}  


public String login( @RequestParam MultipartFile file, Model model) {  
 ...........................   
}  

Where the name of the file must be guaranteed with < Input type = file> Then the property values remain the same, if more than one file upload, then consider using


public String login(@Valid UserInfo userInfo, BindingResult result, @RequestParam MultipartFile[] files, Model model) {   
 ...........................    
}  


public String login(@Valid UserInfo userInfo, BindingResult result, @RequestParam MultipartFile[] files, Model model) {  
 ...........................   
}  

Single files can be omitted @requestparam multiple files cannot be omitted

The second way:


public ModelAndView handleRequest(HttpServletRequest request,      
            HttpServletResponse response) throws Exception {      
        //Transition to MultipartHttpRequest: & NBSP;        
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;      
        //Obtaining documents: & NBSP;        
        MultipartFile file = multipartRequest.getFile(" file ");     
    } 


public ModelAndView handleRequest(HttpServletRequest request,     
            HttpServletResponse response) throws Exception {     
        //Transition to MultipartHttpRequest: & NBSP;      
        MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;     
        //Obtaining documents: & NBSP;      
        MultipartFile file = multipartRequest.getFile(" file ");    
    }  

You can also get the file

The first thing you need to configure is to add two jars:

(link: http://xiazai.jb51.net/201401/yuanma/commons-io-2.1 (jb51.net). The jar)


Related articles: