Springmvc Perfect Solution Multipart/form-data Way Submit request the problem that the Servletrequest.getparameter method gets no arguments in the Filter

Source: Internet
Author: User

Problem:

When you upload a file using the header of Contenttype=multipart/form-data as a file stream, if you use filter in your code, you will be unable to use filter The Servletrequest.getparameter method does not take the arguments that are submitted together,


By looking at MVC's source, we found two classes,

1.1. The Org.springframework.web.multipart.MultipartHttpServletRequest.Java class (GetParameter of this class) has been implemented and ServletRequest method to obtain parameters for Multipart/form-data and Multipart/form-data method uploads.

1.2. Org.springframework.web.multipart.MultipartResolver.java class for creating Multiparthttpservletrequest.java

How to use

2.1 First Configure Multiparthttpservletrequest in your spring configuration file

[HTML]   View plain  copy <bean id= "Multipartresolver"             class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "            p:defaultencoding= "UTF-8" >            <property name= "Maxuploadsize" >                <value>104857600</value>            </property>           <property name= " Maxinmemorysize ">               < value>4096</value>           </property>    </bean>  

Note: This bean build needs to be placed in Application-context, and if placed in Dispatcher-servlet, the bean initialization is unsuccessful. Because the first servlet Bean is two separate from the Application Bean container, the servlet can look for application, but not the other way around. And that interceptor need to have a configuration, the Configuration Manager This bean is initialized in application, so you need to put this interceptor also in application


2.2 Inject multiparthttpservletrequest into your filter.

[Java] view plain copy//used to create multiparthttpservletrequest private multipartresolver multipartresolver = null; @Override public void init (Filterconfig arg0) throws Servletexception {//injected bean servletcontext context = Arg0.getservletcontext (); ApplicationContext CTX = webapplicationcontextutils.getwebapplicationcontext (context);    Multipartresolver = (multipartresolver) ctx.getbean ("Multipartresolver"); }

2.3 Replace your servletrequest with multiparthttpservletrequest in your topmost interceptor filter.

[Java]   View plain  copy/**   *  get  request   * @ param req   *  @return    */   private servletrequest getrequest ( Servletrequest req) {       String enctype =  Req.getcontenttype ();       if  (Stringutils.isnotblank (enctype)  & & enctype.contains ("Multipart/form-data"))             //  return  MultipartHttpServletRequest  parameters for obtaining  multipart/form-data  Submit request   Upload            return multipartresolver.resolvemultipart ( HttpServletRequest)  req);       else             return req;  }  

2.4 gives  chain.dofilter the  multiparthttpservletrequest obtained by Getrequest (req) above 2.3 ( Multiparthttpservletrequest, RESP); Allow the request to continue execution. All subsequent interceptors and controller can continue with the get parameter

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.