Recent projects used to implement multiple files and some information upload,
and background receive and read use of spring and SPRINGMVC related framework.
Not much to say nonsense, directly on the example (this has the key code)
First, the front page
<form action= "/uploader/upload/uploadfiles" method= "post" enctype= "Multipart/form-data" ><input type= "file "Size=" "Name=" Uploadify "multiple=" true "id=" Files "/> <button id=" uploadfiles "> Upload files </button> < Input type= "hidden" id= "username" name= "username"/> <input type= "hidden" id= "directory" name= "directory"/> </form>
first of all, explain the address in action to accept the address of the file in my background,
because uploading files, so enctype = "Multipart/form-data" is a must.
Select multiple file uploads at the same time, without the use of various controls, multiple set to "true" can be.
In addition, it is important to upload the name attribute in the file input , which will be used by the background fetch file.
below the background code, username and directory can be obtained directly in the parameters
@RequestMapping (value = "/uploadfiles", method = requestmethod.post, consumes = "multipart/*") publicvoid uploadfilesfromhtml (@RequestParam (defaultvalue = "") string username, @RequestParam (defaultvalue = "") String Directory,multiparthttpservletrequest request) throws fileuploadexception, ioexception { multivaluemap<string,multipartfile> map = request.getmultifilemap ();// in order to obtain the file, this class is necessary List< Multipartfile> list = map.get ("uploadify");// get a list of files stringpath= getfilepath (username,directory), //ignored, the address that was finally uploaded for the file List<String>filenameList= newArrayList<> (// //); Storage of images for  (multipartfile mfile : list) { Stringoriginalfilename= mfile.getoriginalfilename ();//Get file name filenamelist.add (OriginalFilename); byte[] bytes = mfile.getbytes ();//Get byte array stringfilepath= path+ file.separator+ originalfilename; fileoutputstreamfos= newfileoutputstream (NewFile (FilePath)); //Write to File fos.write (bytes); fos.flush (); fos.close (); } }
This article is from the "Maple Leaf is not red" blog, please be sure to keep this source http://itlearninger.blog.51cto.com/12572641/1913131
Multiple file uploads and Java background acceptance