File Uploading Using Struts.

Hello all,

          I am developing an application using struts. I need to upload multiple files from web interface. For example, if user selects to upload two files, i have to upload both of them, does anyone have an abstract idea of its possibility, because even Yahoo and Gmail requires one to browse twice to upload 2 files., so if any suggestions out there... i would love to hear.

Thank You.
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

bloodredsunConnect With a Mentor Commented:
Fileupload from jakarta commons can handle multiple files in one pass but I've never used it inside Struts...
>> but I've never used it inside Struts...

Struts has its own one ;-)

Just use a normal collection object, but use a collection of org.apache.struts.upload.FormFile objects...

Shouldn't be too hard...
I knew that there's one in the examples (struts-upload) but I didn't know whether it could handle multiple files in one go...
The new generation of project management tools

With’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

One file per FormFile object, but there should be no worries with a collection of them...

I don't think*


(*  I have never tried this, and it may not work) ;-)
letsbedecentAuthor Commented:
Ok then i guess i have to try it.... and face problems  :((

I had bad experience (while ago though) with struts indexed properties and FormFile so I used bloodredsun aproach and
handled the request using the jakarta library within the Action, which was very simple to do.
letsbedecentAuthor Commented:
Can you provide a rough idea of how to achieve this !! I have no idea of how to achieve this.

I thought of using html:file tag at the front end, then using the property in the form i can upload the file....

but using jakarta library, what should be on the jsp and what should be on the form and what logic in the action handler !!??

aozarovConnect With a Mentor Commented:
The jsp side can remain the same.
In the Action you can add the following (extracted from :

// Create a new file upload handler
DiskFileUpload upload = new DiskFileUpload();

// Parse the request (add FileItems to the list)
List items = upload.parseRequest(request); // request is the HttpRequest

// Process the uploaded items
Iterator iter = items.iterator();
while (iter.hasNext())
    FileItem item = (FileItem);

    if (item.isFormField())
        // process FormField (not a File)
        String name = item.getFieldName();
        String value = item.getString();
        //  process upload file (FileItem)
        String fieldName = item.getFieldName();
         String fileName = item.getName();
         String contentType = item.getContentType();
         boolean isInMemory = item.isInMemory();
        long sizeInBytes = item.getSize();

All Courses

From novice to tech pro — start learning today.