Multiple file uploads and Java background acceptance

Source: Internet
Author: User

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&nbsp (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

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.