SPRINGMVC File upload multiple file upload instance _java

Source: Internet
Author: User
Tags exception handling

The dispatcherservlet must be clearly told how to handle multipartrequest.
Springmvc file upload is provided in the following ways
Configure Xxx-servlet.xml and add the following code:

Copy Code code as follows:

<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>

Copy Code code as follows:

<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>

Note that the size of the file here is actually the total size of the file
If the file size is configured, you need to configure exception information control
So you need to configure an exception display

Copy Code code as follows:

<!--Springmvc throws Org.springframework.web.multipart.MaxUploadSizeExceededException--> when it exceeds the upload file limit
<!--This exception is springmvc when checking the uploaded file information, and has not yet entered the controller method-->
<bean id= "Exceptionresolver"
class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
<property name= "Exceptionmappings" >
<props>
<!--automatically jumps to/web-inf/jsp/error_fileupload.jsp page when Maxuploadsizeexceededexception exception is encountered-->
<prop
key= "Org.springframework.web.multipart.MaxUploadSizeExceededException" >error_fileupload</prop>
</props>
</property>
</bean>

Copy Code code as follows:

<!--Springmvc throws Org.springframework.web.multipart.MaxUploadSizeExceededException--> when it exceeds the upload file limit
<!--This exception is springmvc when checking the uploaded file information, and has not yet entered the controller method-->
<bean id= "Exceptionresolver"
class= "Org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
<property name= "Exceptionmappings" >
<props>
<!--automatically jumps to/web-inf/jsp/error_fileupload.jsp page when Maxuploadsizeexceededexception exception is encountered-->
<prop
key= "Org.springframework.web.multipart.MaxUploadSizeExceededException" >error_fileupload</prop>
</props>
</property>
</bean>

Or lazy point, do not set the size, the final default is the value is not limited. If you do not want to control the return information, you can consider returning the specified format data in the exception handling that should be in place, such as JSON

Configure the page as always on the need to add to form form: enctype= "Multipart/form-data"
And then there's the action that needs to be handled, and there are two ways
The first way:

Copy Code code as follows:

Public String Login (@RequestParam multipartfile file, model model) {
..................
}

Copy Code code as follows:

Public String Login (@RequestParam multipartfile file, model model) {
..................
}

The name of the file must be consistent with the attribute value in the <input type=file>, and if multiple files are uploaded, consider using the

Copy Code code as follows:

Public String Login (@Valid UserInfo UserInfo, bindingresult result, @RequestParam multipartfile[] files, model model) {
..................
}

Copy Code code as follows:

Public String Login (@Valid UserInfo UserInfo, bindingresult result, @RequestParam multipartfile[] files, model model) {
..................
}

Single file can be omitted @RequestParam multiple files are not omitted

The second way:

Copy Code code as follows:

Public Modelandview HandleRequest (HttpServletRequest request,
HttpServletResponse response) throws Exception {
Transition to Multiparthttprequest:
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Get files:
Multipartfile file = multipartrequest.getfile ("file");
}

Copy Code code as follows:

Public Modelandview HandleRequest (HttpServletRequest request,
HttpServletResponse response) throws Exception {
Transition to Multiparthttprequest:
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Get files:
Multipartfile file = multipartrequest.getfile ("file");
}

This also allows you to get to the file

In fact, the first need to configure is to add two jar packages:

Commons-fileupload-1.2.2.jar
Commons-io-2.1.jar

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.