1. Add code in Servlet-dispatcher.xml
< Bean id="Multipartresolver"class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver "/>
You can also add related properties to your needs< Property name="Maxuploadsize" value="2097152"></ Property>
2, adding dependent jar files
<dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId>< version>2.4</version></dependency><dependency><groupid>commons-fileupload</ groupid><artifactid>commons-fileupload</artifactid><version>1.3.1</version></ Dependency>
3, Write Uploadcontroller
@RequestMapping (value= "/upload", method=requestmethod.post) public String processupload (@RequestParam ("name") String name, @RequestParam ("file") Multipartfile file) throws IOException { log.info ("file '" + File.getoriginalfilename () + "' uploaded successfully"); if (!file.isempty ()) { try { byte[] bytes = File.getbytes (); Bufferedoutputstream stream = new Bufferedoutputstream (new FileOutputStream (name)); Stream.Write (bytes); Stream.Close (); Return "You successfully uploaded" + name + "!"; } catch (Exception e) { return "failed to upload" + name + "= +" + e.getmessage (); } } else { return ' failed to upload ' + name + ' because the file was empty. '; } }
4, using restclient test upload
Background prompt:
[2015-08-18 11/:36/:12] INFO Com.kaishuhezi.api.hardware.log.controller.LogController (line/:40)-file ' 78a0f7dcjw1e1bvyuzt1jj.jpg ' Uploaded successfully
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Spring Uploading Files