Multi-file uploading using Jakarta commons FileUpload component

Source: Internet
Author: User
Tags file upload
Multi-file uploading using Jakarta commons FileUpload ComponentOnce wrote an article "Use Multipartfile implementation file upload", but recently in the work, I need to upload multiple files at the same time, it seems to use multipartfile can not be competent, so thought of the Jakarta Commons. Jakarta Commons is really good, there are so many open source and easy to use Dongdong, too convenient. Well, less gossip, start by introducing the Jakarta Commons FileUpload component.
First of all, need to download the latest jar package and its dependent jar package on its official web, then can refer to its user guide, very simple, believe that everyone can understand, here is the main content to mention (equivalent to a cut translation).
I. Rationale
The FileUpload component sees all the elements submitted by the page (ordinary form fields, such as text and file field file) as the same fileitem, so that the request requested by the upload page is an ordered combination of Fileitem, The FileUpload component resolves the request and returns a Fileitem of one. For each fileitem,fileupload component, you can determine whether it is a normal form form field or a file field. Depending on the type, take different actions--if it is a form field, read its value, and if it is a file field, save the file to the server's hard disk or in memory.
Two. Concrete implementation
For a HttpRequest request, we want to determine if the request is a file upload request//Check that we have a file upload request
Boolean ismultipart = servletfileupload.ismultipartcontent (request); However, personally feel that this method is generally not necessary, we generally need to upload the FileUpload component to handle request requests.
If it is a request for a file upload, how do we parse it?
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 to the Fileitem list, the processing of each item to consider is the standard Form form field or upload Files file field, and then do different processing, can be implemented in the following ways://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 form field, we can get information about the domain in the following ways://Process a regular form field
if (Item.isformfield ()) ... {
String name = Item.getfieldname ();
String value = item.getstring ();
...
And for a upload file, we can get information about it in the following ways://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, for an uploaded file, we typically don't have it in memory unless it's small enough. We usually save it to the hard disk, use the following methods to save the uploaded file to the server hard disk: File UploadedFile = new file (...);
Item.write (UploadedFile); OK, here, the basic API of the FileUpload component has been introduced, we should have a little outline.
In the next article, I'll wrap the Commons FileUpload component slightly 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.