Select multiple files to upload simultaneously (take advantage of the multiple attribute of the INPUT element)

Source: Internet
Author: User

Web page:

<! DOCTYPE html><html><head><meta charset="UTF-8"><title>Insert Title here</title></head><body>    <form Action="Upload.do" method="POST" enctype=" Multipart/form-data ">        <p>A file to choose to upload</P>        <input type="text" name="Reck" value= ""/><br/>         <!--can choose one or more images to upload -        <input type="file" name="Files" multiple/><br/>         <!--only one file per INPUT element can be selected --<!--<input type= "file" name= "files"/><br/> <input type= "file" name= "Files"/>&l t;br/> <input type= "file" name= "files"/><br/> -        <input type="Submit" value="Submit"/>    </form></body></html> 

In Spring, configure:

    <!--======== File upload ======= --    <!--200*1024*1024 the 200M resolvelazily property is enabled to defer file parsing to capture file size anomalies--    <bean id= "multipartresolver"class=" Org.springframework.web.multipart.commons.CommonsMultipartResolver ">                < property name="Maxuploadsize" value="209715200" />         < property name="defaultencoding" value="UTF-8" / >        < property name="resolvelazily" value="true" />    </Bean>

Java code:

 PackageOrg.wxy.demo.action;ImportJava.io.File;ImportJava.io.IOException;ImportJavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;ImportOrg.springframework.web.bind.annotation.ExceptionHandler;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.RequestParam;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportOrg.springframework.web.multipart.MaxUploadSizeExceededException;ImportOrg.springframework.web.multipart.MultipartFile;/** * * < A sentence function brief ><p> * Multipartfile class Common methods: * String getContentType ()//Get file MIME type * inputstre Am getInputStream ()//after going to file stream * string getName ()//Get the name of the file component in the form * string Getoriginalfilename ()//Get on filename of the file * Long GetSize ()//Gets the byte size of the file, the unit byte * Boolean isEmpty ()//Is empty * void TransferTo (F Ile dest)//save to a target file. * * @author wangxy * @param <E> * * @see [Related Classes/methods] * @since [Product/Module Version] */@ControllerPublic class Fileupdownctrl<E> {    /** * Upload files * @param file * @return * *    @RequestMapping(value="/upload.do") Public String FileUpload (@RequestParam("Files"Multipartfile[] Files,httpservletrequest request) {String rect = (string) Request.getattribute ("Rect"); System.out.println (rect);//To determine that the file array cannot be empty and is longer than 0        if(files!=NULL&& files.length>0){//loop to get files in file array             for(int i =0; i<files.length;i++) {Multipartfile file = Files[i];Try{//Get access PathString FilePath = Request.getsession (). Getservletcontext (). Getrealpath ("/") +"upload/"+ File.getoriginalfilename ();//Dump FilesFile.transferto (NewFile (FilePath)); }Catch(IOException e)                {E.printstacktrace (); }              }//redirect            return "Redirect:/list.do"; }//redirect        return "Redirect:/fail.html"; }@RequestMapping(value="/list.do")@ResponseBodyPublic String list (httpservletrequest request,httpservletresponse response) {String FilePath = request.getsession ( ). Getservletcontext (). Getrealpath ("/") +"Upload"; file[] Files =NewFile (FilePath). Listfiles (); StringBuilder FileNames =NewStringBuilder (); for(File f:files)            {System.out.println (F.getname ()); Filenames.append (F.getname () +"\ n"); }returnFilenames.tostring (); }/** * configured <property name= "resolvelazily" value= "true"/> * only valid * @author wangxy * * @param ex * /    @ExceptionHandlerpublic void Doexcepiton (Exception ex) {if(ex instanceof Maxuploadsizeexceededexception) {System.out.println ("The file is too large"); }    }}

Select multiple files to upload simultaneously (take advantage of the multiple attribute of the INPUT element)

Related Article

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.