- Necessary prerequisites for File upload
- Provide form form, submit by method must be post
- The enctype of the form form (the MIME type that tells the server request body) must be:multipart/form-data
- Provides <input type= "file" > uploaded input fields
- File uploads with third-party upload components
- FileUpload is an upload component provided by Apache's Commons component. Its main job is to help us resolve request.getinputstream ().
- Import the relevant Jar
L Commons-fileupload.jar, Core pack
L Commons-io.jar, Dependency package
- the core class of FileUpload
- Diskfilefactory
- Servletfileupload
- 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