A little problem at work, take a note.
Implement SPRINGMVC + JSP + Ajax upload file
Html
<Body> <formID= "MyForm"Method= "POST" >Login Name<inputtype= "text"name= "LoginName" /> <BR>Uploading Recordings<inputtype= "File"name= "Record" /> <inputtype= "button"onclick= "Doupload ()"value= "Submit" /> </form> </Body>
Javascript
functiondoupload () {varFormData =NewFormData ($ ("#myform") [0]); $.ajax ({URL:' Insert/fileupload.action ', type:' POST ', Data:formdata, async:false, Cache:false, ContentType:false, ProcessData:false, Success:function(returndata) {alert (returndata); }, Error:function(returndata) {alert (returndata); } }); }
Springmvc.xml
<!--config file upload parser - <BeanID= "Multipartresolver"class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver"> < Propertyname= "Defaultencoding"value= "Utf-8"/> < Propertyname= "Maxuploadsize"value= "10485760000"/> < Propertyname= "Maxinmemorysize"value= "40960"/> </Bean>
Java
Requestmapping ("FileUpload") Public voidFileUpload (httpservletrequest request,httpservletresponse response,string loginName)throwsException {//get the path to the saved file on the serverString path = Request.getsession (). Getservletcontext (). Getrealpath ("") + "\\upload\\record\\"; SYSTEM.OUT.PRINTLN (path); //Get parserCommonsmultipartresolver resolver =NewCommonsmultipartresolver (Request.getsession (). Getservletcontext ()); //determine if it is a file if(Resolver.ismultipart (Request)) {//to convertMultiparthttpservletrequest multirequest =(multiparthttpservletrequest) (request); //get all file namesIterator<string> it =Multirequest.getfilenames (); while(It.hasnext ()) {//fetch files by file nameMultipartfile file =Multirequest.getfile (It.next ()); String FileName=File.getoriginalfilename (); String LocalPath= Path +FileName; //Create a new file object that requires a parameter when it is created, where the file needs to be savedFile NewFile =NewFile (LocalPath); //The uploaded file is written to the specified fileFile.transferto (NewFile); } }
Springmvc+jsp+ajax Uploading Files