1. configuration file section
Add the following code to the spring configuration file
<class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" ID = "Multipartresolver" />
2. Jar Package Used
Commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar3, the code part--Upload to the local folder
Public classUploadcontroller {//set the path of the fileFile FilePath =NewFile ("D:/workspace-letter"); @RequestMapping (Value= "/upload", method =requestmethod.post) @ResponseBody//can be successfully transferred PublicString Upload (@RequestParam multipartfile file)throwsIOException {//get file Original nameString filename =File.getoriginalfilename (); //get file name extensionString fileextension = filename.substring (Filename.lastindexof ("."))); //img filename prefixFile tempfile = File.createtempfile ("img", Fileextension,filepath); Try{InputStream in=File.getinputstream (); OutputStream out=NewFileOutputStream (tempfile); Ioutils.copy (in, out); }Catch(Exception e) {e.printstacktrace (); } returnfilename; }}
4. Upload to Server
@Controller Public classUploadcontroller {@RequestMapping (value= "/upload", method =requestmethod.post) @ResponseBody PublicString Upload (@RequestParam multipartfile file, httpservletrequest request)throwsIOException {//set the upload path for the serverFile FilePath =NewFile ("//sharedata//p4"); String filename=File.getoriginalfilename (); String fileextension= Filename.substring (Filename.lastindexof ("."))); File Tempfile= File.createtempfile ("Letter", Fileextension,filepath); Try{InputStream in=File.getinputstream (); OutputStream out=NewFileOutputStream (tempfile); Ioutils.copy (in, out); } Catch(Exception e) {e.printstacktrace (); } returnFilepath.getpath () + "/" +filename; }}
Spring Upload file