Spring MVC uploads files

Source: Internet
Author: User
Tags tag name

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

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.