JSP page
<form action= "${pagecontext.request.contextpath}/user/upload" method= "post" enctype= "Multipart/form-data" > Attach 1:<input type= "file" name= "attachs"/><br/> attach 2:<input type= " File "Name="attachs"/><br/> <button type=" Submit "> Submit </button></form>
Controller.java Receive data (code below)
@RequestMapping (value="/upload", method=requestmethod.post) PublicString Upload (@RequestParam ("Attachs") multipartfile[] Attachs, httpservletrequest req) {String Realpath=req.getsession (). Getservletcontext (). Getrealpath ("/resources/upload");//save path to System. out. println (Attachs.length); System. out. println (Realpath); for(Multipartfile attach:attachs) {System. out. println (attach); if(!Attach.isempty ()) { file file =new File (realpath+ "/" +attach.getoriginalfilename ()); Try{
The underlying data IO transmission attach.transferto (file); } Catch(IllegalStateException |IOException E) {E.printstacktrace (); } } } return "Welcome"; }
Dispatcherservlet-servlet.xml Code:
<!--configuration multipartresolver for uploading files, using spring Commonsmultiparresolver-<bean id="Multipartresolver" class="Org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxuploadsize"Value="5000000"/> <property name="defaultencoding"Value="UTF-8"/> </bean>
spriing MVC uploads multiple files