I tested the SPRINGMVC package to the good parser Multipartfile, which contains the information to upload the file
1. Forms
<!--Note: 1 must be a post to submit the value of the 2.enctype attribute must be Multipart/form-data - <formAction= "/upload/upload"Method= "POST"enctype= "Multipart/form-data"> <inputtype= "File"name= "AA" /> <inputtype= "Submit"value= "Submit" /> </form> </Body>
2. Writing Controller
1) upload to Local disk
/*** Upload to Local Disk *@paramAA receives NAME=AA, the form element that contains the information of the file *@return */@RequestMapping ("/upload") PublicString upload (multipartfile aa) {Try { //File UploadAa.transferto (NewFile ("d:\\" +aa.getoriginalfilename ())); } Catch(Exception e) {e.printstacktrace (); } return"Index"; }
2) upload to Tomcat
/*** Upload to the upload folder of the Web project, first create a upload folder in the project *@paramAA *@paramRequest *@return */@RequestMapping ("/upload1") PublicString upload1 (multipartfile aa,httpservletrequest request) {System.out.println (Aa.getoriginalfilename ()); Try { //path to File uploadString Realpath = Request.getsession (). Getservletcontext (). Getrealpath ("/upload"); Aa.transferto (NewFile (Realpath,aa.getoriginalfilename ())); } Catch(Exception e) {e.printstacktrace (); } return"Back/ok"; }
3.SPRINGMVC configuration file
<!--file upload Bean ID must be called Multipartresolver, and cannot be changed Springmvc file upload by default, but can set the size of the file upload - <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> < Propertyname= "Maxuploadsize"value= "20971520"/> </Bean>
Springmvc File Upload