SPRINGMVC Learning--File upload

Source: Internet
Author: User

    • Brief introduction

  File upload is one of the common requirements in web development, SPRINGMVC file upload is integrated, it can be easily and quickly developed.

    • resolution of multi-part types in SPRINGMVC

When you submit data for enctype= "Multipart/form-data" in a page form , you need to SPRINGMVC parse the data for the multipart type. Configure The multipart type resolver in springmvc.xml .

1 <!--File Upload -2     <BeanID= "Multipartresolver"3 class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver">4         <!--set the maximum size of the uploaded file to 5MB -5         < Propertyname= "Maxuploadsize">6             <value>5242880</value>7         </ Property>8     </Bean>
    • Add jar to upload image

Edge Resolution internal Use the jar below to upload pictures.

 

    • Create a picture virtual directory store picture

Configure the picture virtual directory on Tomcat and add it under Tomcat under conf/server.xml :

1 <  docBase= "F:\develop\upload\temp"  path= "/pic"  reloadable = "false" />

Access http://localhost:8080/pic to access images under F:\develop\upload\temp.

    • Controller code
1 //Product Modification Submission2@RequestMapping ("/edititemsubmit")3      PublicString edititemsubmit (items items, Multipartfile picturefile)throwsexception{4         5         //Original file name6String Picturefile_name =picturefile.getoriginalfilename ();7         //New file name8String NewFileName = Uuid.randomuuid (). toString () +picturefile_name.substring (Picturefile_name.lastindexof ("."));9         Ten         //Upload Image OneFile Uploadpic =NewJava.io.File ("f:/develop/upload/temp/" +newfilename); A          -         if(!uploadpic.exists ()) { - uploadpic.mkdirs (); the         } -         //write files to disk - Picturefile.transferto (uploadpic); -     ... +}

SPRINGMVC Learning--File upload

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.