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