Spring MVC uploads files

Source: Internet
Author: User

Step one: Springmvc.xml join the configuration Multipartresolver processor. You can add attribute restrictions to uploaded files here.

<bean id="multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipart Resolver ">

<property name="defaultencoding" value="Utf-8"></property>

<!--The maximum number of uploaded files--

<property name="maxuploadsize" value="10485760000"></property>

<!--cache Size-

<property name="maxinmemorysize" value="40960"></property>

</bean>

Step two: Prepare the foreground form. Note enctype= "Multipart/form-data" and <input type= "file" name= "* * *"/>

<form action="udload/upload.do" method="POST" enctype="Multipart/form-data " >

Select: <input type="file" name="myfile" id= "myfile" ><br/>

<input type=" submit" value=" submission">

</form>

Step three: Add the Multipartfile parameter to the controller's method. This parameter is used to receive the contents of the file component in the form

@RequestMapping ("/upload")

Public String upload (model model, multipartfile multipartfile)

Throws IOException {

Gets the path of the uploaded file save

String path = Request.getsession (). Getservletcontext (). Getrealpath ("Upload");

Get the name of the uploaded file

String filename = multipartfile. Getoriginalfilename ();

Create a folder upload

File TargetFile = new file (path, filename);

Determine if the folder already exists, and if it already exists, re-build

if (!targetfile.exists ()) {

Targetfile.mkdirs ();

}

multipartfile. TransferTo (TargetFile);

return "Votecount/result";

}

Spring MVC uploads files

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.