Commons-fileupload of File Upload (i)

Source: Internet
Author: User

First, Brief introduction
Using the most extensive Java file Upload component, struts itself uses this package to handle file uploads. its rationale:
The FileUpload component considers all the elements of a page submission (normal form form fields, such as text and file field files) to be the same fileitem, so that the request requests submitted by the upload page are an ordered combination of Fileitem, The FileUpload component resolves the request and returns a single fileitem. And for each Fileitem,fileupload component can determine whether it is a normal form form field or file domain, so that according to different types, take different actions-if it is a form field, read out its value, if it is a file domain, save the file to the server hard disk or in memory.

Second, the introduction of the next commons-fileupload upload process:
the. jar file to be introduced first:
Commons-fileupload-1.2.1.jar
Commons-io-1.2.jar ( This two files can go to the official website: http://commons.apache.org/(there will also be a Help document API) download)
Second, it is worth noting that any form that is to upload a file must have the Enctype property set, and the value of the property must be Multipart/form-data, and the request method must be post!!!!

1. Check if the request contains multipart content (Ismultipartcontent (request) is a static method of it)

Servletfileupload.ismultipartcontent (Request)

2, if there is, the build Diskfileitemfactory factory will be related to the settings (do not know the case can also not set)

         Diskfileitemfactory factory = new Diskfileitemfactory ();
       //maximum size that'll be stored in memory
        factory.setsizethreshold (        //location to Save data is larger than maxmemsize.
       factory.setrepository (NewFile ("d:/"));//  Set the temp directory  

        factory .setheaderencoding ("UTF-8");//setting Character set   preventing Chinese file names from garbled

3. Generate and upload the Servletfileupload class, and pass the Diskfilefactory factory to it and configure the Servletfileupload

Create a new file upload handler
Servletfileupload upload = new Servletfileupload (factory);
Upload.setfilesizemax (3*1024*1024);//Set Upload file size 3M
Upload.setsizemax (6*1024*1024);//Set Request total file size 6M

4. Get a list of uploaded files from request and get their iterators

Parse the request to get file items.
list<fileitem> Fileitems =upload.parserequest (Request);

Process the uploaded file items
Iterator i = Fileitems.iterator ();

5. Processing files: Writing or other operations

while (I.hasnext ()) {
                fileitem fi = (Fileitem) i.next ();
               if ( Fi.isformfield ()) {//When it is a form field, the processing
                    //Get the uploaded file parameters
                    string FieldName = Fi.getfieldname ();//Gets the value of the element's Name property

String fieldvalue=fi.getstring ("Utf-8");//Gets the value of the element
String fileName = Fi.getname ();//This is the form field, so the result is null


} else {//when it is a file field, save the file to your hard disk

String BasePath = Request.getrealpath ("/upload");

String ContentType =fi.getcontenttype ();
Boolean isinmemory = Fi.isinmemory ();
Long sizeInBytes =fi.getsize ();

String fieldName =fi.getfieldname ();//Gets the name of the element

                      string filename = fi.getname ();//Get the uploaded file name
                         file = new File (basepath+ "/" +filename);

Fi.write (file);
}

Out.println ("uploaded filename:" + filename + "<br>");
}
}

}

Description

Fileitem interface is the encapsulation of user uploaded files

Diskfileitemfactory implements the Fileitemfactory interface, the Main method is Publicfileitem CreateItem (string fieldName, String ContentType, Booleanisformfield, String FileName)

Servletfileupload inherit from FileUpload, and fileupload inherit from Fileuploadbase, function: Parse incoming request object, get file list fileitemiterator ...

Commons-fileupload of File Upload (i)

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.