Uploading of files

Source: Internet
Author: User

    • Necessary prerequisites for File upload
    1. Provide form form, submit by method must be post
    2. The enctype of the form form (the MIME type that tells the server request body) must be:multipart/form-data
    3. Provides <input type= "file" > uploaded input fields
    • File uploads with third-party upload components
    1. FileUpload is an upload component provided by Apache's Commons component. Its main job is to help us resolve request.getinputstream ().
    2. Import the relevant Jar

L Commons-fileupload.jar, Core pack

L Commons-io.jar, Dependency package

    • the core class of FileUpload
    1. Diskfilefactory
    2. Servletfileupload
    3. Fileitem
1 ImportJava.io.File;2 ImportJava.util.HashMap;3 Importjava.util.List;4 ImportJava.util.Map;5 6 Importjavax.servlet.http.HttpServletRequest;7 8 ImportOrg.apache.commons.fileupload.FileItem;9 Importorg.apache.commons.fileupload.disk.DiskFileItemFactory;Ten ImportOrg.apache.commons.fileupload.servlet.ServletFileUpload; One Importorg.apache.commons.io.FilenameUtils; A  -  Public classMyfileupload { -  the      Public StaticMap<string,string[]>getfileupload (HttpServletRequest request) { -         //1 Getting form data -         //*1.1 Creating a factory class Diskfileitemfactory object -Diskfileitemfactory factory =Newdiskfileitemfactory (); +         //*1.2 using factory creation to parse objects -Servletfileupload fileUpload =Newservletfileupload (factory); +         //*1.3 using the parser to parse the request object A         //data used to encapsulate normal form items atmap<string, string[]> map =NewHashmap<string,string[]>(); -          -         Try { -list<fileitem> list =fileupload.parserequest (request); -              for(Fileitem item:list) { -                 if(Item.isformfield ()) { in                     //if there's normal data in the package, -String name = Item.getfieldname ();//Field name toString value = item.getstring ("UTF-8");//the value of the field +Map.put (Name,Newstring[]{value}); -}Else{                     the                     //inside is the file form data *String filename = Item.getname ();//file name of the upload $String extension = filenameutils.getextension (filename);//file name extensionPanax Notoginseng                     //The extension of the uploaded file cannot be jsp and EXE -                     if(! (Extension.equals ("jsp") | | Extension.equals ("EXE"))){ the                         //Create a directory +File Storedir =NewFile (Request.getservletcontext (). Getrealpath ("/web-inf/upload")); A                         if(!storedir.exists ()) { theStoredir.mkdirs ();//Create directory If directory does not exist +                         } -                         //working with file names $                         if(FileName! =NULL&& (! "". Equals (filename)) { $filename =filenameutils.getname (filename); -}Else{ -                             Continue; the                         } - Wuyi                         //Catalog Break up theString Childdir =Makechilddir (storedir,filename); -filename = childdir+file.separator+filename; Wu                         //System.out.println (Storedir);  -                         Try { AboutItem.write (NewFile (Storedir,filename));//!!!!!!! Storage Path $ Item.delete (); -}Catch(Exception e) { - e.printstacktrace (); -                         } AMap.put (Item.getfieldname (),Newstring[]{filename}); +                     } the                 } -             } $}Catch(Exception e) { the e.printstacktrace (); the         } the         returnmap; the     } -     Private Staticstring Makechilddir (File storedir, string filename) { in         intHashcode = Filename.hashcode ();//returns the 32-bit hashcode code of a string conversion theString code = integer.tohexstring (hashcode);//convert Hashcode to 16-binary characters the  AboutString childdir = Code.charat (0) +file.separator+code.charat (1);//A/ b the         //Create the specified directory theFile File =NewFile (Storedir,childdir);//Create a new Childdir path based on the path of Storedir the         if(!file.exists ()) { + file.mkdirs (); -         } the         returnChilddir;Bayi     } the  the}

Uploading of files

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.