Use the Jakarta commons fileupload component to upload multiple files

Source: Internet
Author: User
Use the Jakarta commons fileupload component to upload multiple files

I have written an article "using multipartfile to upload files". However, recently, at work, I need to upload multiple files at the same time. It seems that I cannot use multipartfile, so I thought of Jakarta commons. Jakarta commons is really good. It's too convenient to have so many open-source and useful stuff. Let's talk about the Jakarta commons fileupload component.
First, you need to download the latest jar package and its dependent jar package on its official website. Then, you can refer to its user guide. It is very simple. You can understand it, here we will mention the main content (equivalent to a cut translation ).
I. the fileupload component regards all elements submitted by the page (common form fields, such as text and file fields) as the same fileitem, in this way, the request submitted on the upload page is an ordered combination of fileitem. The fileupload component can parse the request and return a fileitem. For each fileitem, The fileupload component can determine whether it is a common form field or a file field, so that different operations can be taken based on different types-if it is a form field, read the value. If it is a file field, save the file to the server hard disk or memory.

II. Specific implementation
For an httprequest request, we need to determine whether the request is a file upload request.

// Check that we have a file upload request
Boolean ismultipart = servletfileupload. ismultipartcontent (request );

However, I personally think this method is generally not required. We generally use the fileupload component to process request requests only when uploading is required.
How can we parse a file upload request? // Create a factory for disk-based file items
Fileitemfactory factory = new diskfileitemfactory ();

// Create a new file upload Handler
Servletfileupload upload = new servletfileupload (factory );

// Parse the request
LIST/** // * fileitem */items = upload. parserequest (request );


So far, we have reached the fileitem list. to process each item, we should consider the standard form field or the file field to be uploaded, and then perform different processing, which can be achieved through the following methods:

// Process the uploaded items
Iterator iter = items. iterator ();
While (ITER. hasnext ())...{
Fileitem item = (fileitem) ITER. Next ();

If (item. isformfield ())...{
Processformfield (item );
} Else ...{
Processuploadedfile (item );
}
}

For a simple form field, we can obtain the information about this field using the following methods:

// Process a regular form field
If (item. isformfield ())...{
String name = item. getfieldname ();
String value = item. getstring ();
...
}

For an upload file, we can obtain the relevant information through the following method:

// Process a File Upload
If (! Item. isformfield ())...{
String fieldname = item. getfieldname ();
String filename = item. getname ();
String contenttype = item. getcontenttype ();
Boolean isinmemory = item. isinmemory ();
Long sizeinbytes = item. getsize ();
...
}

In fact, we generally do not store an uploaded file in memory unless it is small enough. We usually save it to the hard disk. You can save the uploaded file to the server hard disk by using the following methods:

File uploadedfile = new file (...);
Item. Write (uploadedfile );

Now, the basic API of the fileupload component has been introduced. Should everyone have a rough outline?
In the next article, I will package the commons fileupload component and make a simple demo.

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.