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.
letsbedecentAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
bloodredsunConnect With a Mentor Commented:
Fileupload from jakarta commons http://jakarta.apache.org/commons/fileupload/ can handle multiple files in one pass but I've never used it inside Struts...
0
 
TimYatesCommented:
>> 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...
0
 
bloodredsunCommented:
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...
0
The new generation of project management tools

With monday.com’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.

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

I don't think*

Tim

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

0
 
aozarovCommented:
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.
0
 
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 !!??

thanks
0
 
aozarovConnect With a Mentor Commented:
The jsp side can remain the same.
In the Action you can add the following (extracted from http://jakarta.apache.org/commons/fileupload/using.html) :

// 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) iter.next();

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

    }
}
0
All Courses

From novice to tech pro — start learning today.