Springmvc File Upload

Source: Internet
Author: User

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

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.