Use the fileupload Control

Source: Internet
Author: User

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.

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.