Springmvc+jsp+ajax Uploading Files

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.