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