Original address:
Http://www.cnblogs.com/yskcoder/p/4718198.html
It's been a problem in the last two days when I was uploading with spring, and this process has also increased my interest in spring source. There is still a lot to be gained. First
First the upload interface was provided to group A, and there was no multipartresolver configuration in the spring configuration file, background controller Java was obtained as:
Multipartresolver resolver = new Commonsmultipartresolver (Request.getsession (). Getservletcontext ());
Multiparthttpservletrequest multipartrequest = Resolver.resolvemultipart (request);
Multipartfile file = multipartrequest.getfile ("file");
String A1 = Multipartrequest.getparameter ("A1");
It is not a problem to convert the context of request requests to multipartresolver and then convert to multiparthttpservletrequest requests to obtain the corresponding file information by multi request. Background to obtain the corresponding parameters;
Another colleague in the group later used the upload, based on the information on the Internet, that should be added to the configuration file, so the problem came out, but also caused me to record the reason for the blog:
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<!--set the max upload size100mb-->
<property name= "maxuploadsize" >
<value> 10485760000</value>
</property>
<property name= "maxinmemorysize" >
<value> 4096</value>
</property>
</bean>
After this configuration, I in the background of the method to get file for empty caused the upload error, just at the beginning I also wonder, the reason spring should need to upload the configuration, colleagues also feel that I did not carry out configuration kindness to me to modify, but it caused a bug, Offline environment test will remove this configuration feature OK. Well, have done so many years of development, not because fixed bugs without the principle of the problem to see, such a modification even their own.
Searched the data, did the test, has the following summary, has the question to welcome everybody to pat the brick. finally upload the solution to the problem: (i):
1, after the Spring-config configuration
There are two ways to get a background:
1. Specify @requestparam multipartfile file for example: public map<string, object> logsupload (@RequestParam multipartfile file,@ Requestparam (value= "key") String key) parameter
2, convert request to multiparthttpservletrequest Multirequest = ( Multiparthttpservletrequest) (request);
The principle is: Use spring's commosmultipartresolver configuration multipartresolver for file uploads, Dispatcherservlet will call Multipartresolver's The Ismultipart (Request) method checks whether the current WEB requests are multipart types. If so, Dispatcherservlet will invoke the Multipartresolver Resolvemultipart (Request) method, decorate the original request, and return a The multiparthttpservletrequest is used for subsequent processing (the initial httpservletrequest is cynical multiparthttpservletrequest), otherwise, the direct return of the initial HttpServletRequest. In other words, once the request is multipartresolver, it will parse the file in the request without waiting for the subsequent controller to GetFile from the multipartrequest, so after configuring the Multipartresolver , and then by this means
Multipartresolver resolver = new Commonsmultipartresolver (Request.getsession (). Getservletcontext ());
Multiparthttpservletrequest multipartrequest = Resolver.resolvemultipart (request);
is not getting the file, because the controller has helped us to get the conversion directly.
If you use this method to discover that there is no problem, you can take a look at whether the method is configured with a servlet, and if the servlet is configured to not walk this multipartresolver control, it is able to achieve success. (b)
No need for spring-config configuration, directly in the background to get the conversion can be, that is, my original writing.
Multipartresolver resolver = new Commonsmultipartresolver (Request.getsession (). Getservletcontext ());
Multiparthttpservletrequest multipartrequest = Resolver.resolvemultipart (request);
Multipartfile file = multipartrequest.getfile ("file");
String key = Multipartrequest.getparameter ("key");
Then the project specific needs of what kind of change, the integration of their own business discretion. pit, I've fallen into it two times springmvc File Upload file error of exception capture and elegant solution
Article address: http://blog.csdn.net/marvel__dead/article/details/71698636