Springmvc Upload file steps:
First copy of the jar required
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/4C/C5/wKiom1RFGTSAj-GGAAINUI5UoCA367.jpg "title=" 01.jpg "alt=" Wkiom1rfgtsaj-ggaainui5uoca367.jpg "/>
The second needs to be configured in Springmvc (springmvc-servlet.xml):SPRINGMVC When uploading a file, you need to configure the Multipartresolver processor
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" >
<property name= "defaultencoding" value= "UTF-8"/>
<!--Specifies that the total size of the uploaded file cannot exceed 200KB. Note that the limit for the Maxuploadsize property is not for individual files, but for the sum of the capacity of all files.
<property name= "maxuploadsize" value= "200000"/>
</bean>
The third need to add enctype= "Multipart/form-data" in the uploaded form
<form method= "POST" enctype= "Multipart/form-data" action= "upload.do" >
<input type= "File" name= "file"/> <br/>
<input type= "Submit"/> <br/>
</form>
IV Single File upload
@RequestMapping (value = "/upload.do")
Public String Upload (@RequestParam (value = "File", Required = False) Multipartfile file,
HttpServletRequest request, Modelmap model) {
Upload Address
String path = Request.getsession (). Getservletcontext ()
. Getrealpath ("\\upload\\");
String fileName = File.getoriginalfilename ();
File TargetFile = new file (path, new Random (). Nextint (+) + fileName);
if (!targetfile.exists ()) {
Targetfile.mkdirs ();
}
try {
File.transferto (targetfile);
} catch (Exception e) {
E.printstacktrace ();
}
Return "index";
}
Upload of more than five files
Multiple file uploads
@RequestMapping (value = "/upload1.do")
Public String fileUpload (httpservletrequest request, httpservletresponse response) throws Exception {
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
map<string, multipartfile> filemap = Multipartrequest.getfilemap ();
String Ctxpath = Request.getsession (). Getservletcontext ()
. Getrealpath ("\\upload\\");
File File = new file (Ctxpath);
if (!file.exists ()) {
File.mkdir ();
}
System.out.println (Filemap.size ());
String fileName = null;
For (map.entry<string, multipartfile> entity:fileMap.entrySet ()) {
Upload file name
System.out.println ("key:" + Entity.getkey ());
Multipartfile MF = entity.getvalue ();
FileName = Mf.getoriginalfilename ();
File UploadFile = new file (Ctxpath + fileName);
System.out.println (Ctxpath + fileName);
Filecopyutils.copy (Mf.getbytes (), uploadfile);
}
Return "index";
}
Note: The value of the input tag Name property of the page cannot be the same when uploading multiple files
<form method= "POST" enctype= "Multipart/form-data" action= "upload.do" >
<input type= "File" name= "File1"/> <br/>
<input type= "File" name= "File2"/> <br/>
<input type= "Submit"/> <br/>
</form>
This article is from the "Lao Niu Java" blog, please be sure to keep this source http://liuyj.blog.51cto.com/2340749/1566100
Spring MVC uploads files