Use the jar package associated with the component:
Commons-fileupload-1.2.2.jar
Commons-io-2.4.jar
Usage:
Create a disk Factory
DiskFileItemFactory factory = new DiskFileItemFactory();
Set File Cache path
File temp = new File(this.getServletContext().getRealPath(“/”) + “temp”) ;factory.setReponsitory(temp) ;
Create processing tool
ServletFileUpload upload = new ServletFileUpload();
Sets the maximum size of the uploaded file.-1 indicates unlimited size.
Upload. setsizemax (3*1024*1024); // The maximum upload size is 3 MB.
Upload a file and parse all form fields, including common fields and file fields.
Accept all content and store it in the system cache as a list
List<FileItem> all = upload.praseRequest(request) ;Iterator<FileItem> iter = all.iterator() ;
Retrieve through Traversal
While (ITER. hasnext () {fileitem item = ITER. Next (); string fieldname = item. getfieldname (); // get the control name if (! Item. isformfield () {// determines whether it is a common text string name = item. getname (); // get the object name string contenttype = item. getcontenttype (); // obtain the text type long size = item. getsize (); // get the file size file SaveFile = NULL; inputstream input = NULL; // input stream outputstream output = NULL; // output stream // obtain the random name iptiemstamp its = newiptimestamp (request. getremoteaddr (); input = item. getinputstream (); // get the data in the cache // set the file storage path SaveFile = new file (this. Getservletcontext (). getrealpath ("/") + "Upload" + file. separator + its. getiptimerand () + ". "+ item. getname (). split (". ") [1]); Output = new fileoutputstream (SaveFile); int temp = 0; byte data [] = new byte [512]; // set the cache size while (temp = input. read (data, 0,512 ))! =-1) {output. write (data); // save multiple parts} // close the data stream input. close (); output. close ();} else {string value = item. getstring (); // get the value passed in the form }}
Usage
The fileupload control is generally used in the Framework. Otherwise, the smartupload control is generally used to complete the upload function. Because the use of the former is complex and cumbersome.