Discussion:
Looking for Multiple file upload at once example
David Friedman
2004-07-20 14:51:03 UTC
Permalink
RE: Looking for Multiple file upload at once examplePaul,

I'm cc'ing the list so other can learn about this.

The problem is (unless it has been solved and I haven't heard about it) with
the commons file uploads cannot upload multiple files as the very same form
field name. Your velocity template shows the very same name,
"uploadedFiles", used for each file. The last time it was discussed, I
believe the perp (er.. programmer. LOL) used one field and tried to upload
multiple file names in that one field (which is supposedly technically
allowed in file uploads, just not working in any struts-distributed commons
upload version). Your way of using one file name in different fields on the
page is essentially the same thing.

Have you thought of changing those fields to index arrays such as:
Attachment 1: <input type="file" name="uploadedFiles[0]"/>
Attachment 2: <input type="file" name="uploadedFiles[1]"/>
Attachment 3: <input type="file" name="uploadedFiles[2]"/>

Or you could name them randomly such as uploadFiles1, uploadFiles2,
uploadFilesA, uploadFiles27, etc. and use the
CommonsMultipartRequestHandler's getFileElements() method to iterate though
file uploads without worrying about their names. I suppose it all depends
on whether or not you need to I'd recommend using a form with that action,
then casting the form's getMultipartRequestHandler()

method a bit like so:

CommonsMultipartRequestHandler handler = (CommonsMultipartRequestHandler)
form.getMultipartRequestHandler();
HashTable files = handlers.getFileElements();

I haven't used it in a while so if I'm doing a typo on a method name, don't
kill the messenger. :)

Regards,
David

-----Original Message-----
From: Perliti Scorzoni Paolo [mailto:***@siemens.com]
Sent: Tuesday, July 20, 2004 3:42 AM
To: ***@ix.netcom.com
Subject: RE: Looking for Multiple file upload at once example


Hi David.
I'm trying to use a form to upload several files, all at once.
I use Struts and Commons facilities (that is the FormFile interface).
Here's my action form:

public class AttachmentForm
extends ActionForm {

/* Files to upload */
private FormFile[] uploadedFiles;

public FormFile[] getUploadedFiles() {
return uploadedFiles;
}

public void setUploadedFiles(FormFile[] uploadedFiles) {
this.uploadedFiles = uploadedFiles;
}
}

And this is the html code I use to populate the action form (I'm using
Velocity, but I don't think this is really important):

<html>
<body>
...
<form name="uploadForm" method="post" enctype="multipart/form-data"
action="(...my action...)">
...
Attachment 1: <input type="file" name="uploadedFiles"/>
Attachment 2: <input type="file" name="uploadedFiles"/>
Attachment 3: <input type="file" name="uploadedFiles"/>
...
</form>
...
</body>
</html>

Everytime I submit the form I obtain this exception:

javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1254)

org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.j
ava:821)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)



java.lang.IllegalArgumentException: argument type mismatch
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
java.lang.reflect.Method.invoke(Method.java:324)

org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.j
ava:1789)

org.apache.commons.beanutils.PropertyUtils.setNestedProperty(PropertyUtils.j
ava:1684)

org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:17
13)
org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:1019)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1252)

org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.j
ava:821)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

If I adjust the java source to manage one file only (ie. FileForm instead
of FileForm[]) it works, but as I try to use an array I get the Exception.

I use struts 1.1 on Windows XP platform and Tomcat 5.0.24.
I've seen your post about this subject on the jakarta struts-user-list and
I was wondering if you ever had a chance to solve a similar problem.

Is it a bug of commons-beanutils or there's something wrong with my code?

Thanks in advance,
Paul
David Friedman
2004-07-20 17:29:51 UTC
Permalink
RE: Looking for Multiple file upload at once examplePaul,

I believe the problem lies with the Commons FileUpload package. Another way
to go over it is mentioned in this link:
http://nagoya.apache.org/eyebrowse/BrowseList?listName=commons-***@jakarta.
apache.org&by=subject&from=367131&to=367131&first=1&count=2

Essentially, you can iterate over the parameters in the request (where
multiple fields with the same name would show up. do some sort of isFile()
method on that item, and then save it. You would just have to add enough
logic to handle uploaded files with the same form parameter name. The
"Robert Priest" example from the above link is a better explanation than my
writing it down.

Regards,
David

-----Original Message-----
From: Perliti Scorzoni Paolo [mailto:***@siemens.com]
Sent: Tuesday, July 20, 2004 11:08 AM
To: David Friedman
Subject: R: Looking for Multiple file upload at once example


Hi David,
I tried this solution just a few hours ago, before I received your answer!
It worked! The code I used is very similar to the snippet reported in your
mail.
I'd only like to know if the limitation you referred to regards the
BeanUtil package, the FileUpload package or Struts API...
Anyway, thank you very much, your help was VERY appreciated.
Thanks again :-)
Paul

-----Messaggio originale-----
Da: David Friedman [mailto:***@ix.netcom.com]
Inviato: martedì 20 luglio 2004 16.51
A: Perliti Scorzoni Paolo; Struts Users Mailing List
Oggetto: RE: Looking for Multiple file upload at once example


Paul,

I'm cc'ing the list so other can learn about this.

The problem is (unless it has been solved and I haven't heard about it)
with the commons file uploads cannot upload multiple files as the very same
form field name. Your velocity template shows the very same name,
"uploadedFiles", used for each file. The last time it was discussed, I
believe the perp (er.. programmer. LOL) used one field and tried to upload
multiple file names in that one field (which is supposedly technically
allowed in file uploads, just not working in any struts-distributed commons
upload version). Your way of using one file name in different fields on the
page is essentially the same thing.

Have you thought of changing those fields to index arrays such as:
Attachment 1: <input type="file" name="uploadedFiles[0]"/>
Attachment 2: <input type="file" name="uploadedFiles[1]"/>
Attachment 3: <input type="file" name="uploadedFiles[2]"/>

Or you could name them randomly such as uploadFiles1, uploadFiles2,
uploadFilesA, uploadFiles27, etc. and use the
CommonsMultipartRequestHandler's getFileElements() method to iterate though
file uploads without worrying about their names. I suppose it all depends
on whether or not you need to I'd recommend using a form with that action,
then casting the form's getMultipartRequestHandler()

method a bit like so:

CommonsMultipartRequestHandler handler =
(CommonsMultipartRequestHandler) form.getMultipartRequestHandler();
HashTable files = handlers.getFileElements();

I haven't used it in a while so if I'm doing a typo on a method name,
don't kill the messenger. :)

Regards,
David

-----Original Message-----
From: Perliti Scorzoni Paolo [mailto:***@siemens.com]
Sent: Tuesday, July 20, 2004 3:42 AM
To: ***@ix.netcom.com
Subject: RE: Looking for Multiple file upload at once example


Hi David.
I'm trying to use a form to upload several files, all at once.
I use Struts and Commons facilities (that is the FormFile interface).
Here's my action form:

public class AttachmentForm
extends ActionForm {

/* Files to upload */
private FormFile[] uploadedFiles;

public FormFile[] getUploadedFiles() {
return uploadedFiles;
}

public void setUploadedFiles(FormFile[] uploadedFiles) {
this.uploadedFiles = uploadedFiles;
}
}

And this is the html code I use to populate the action form (I'm using
Velocity, but I don't think this is really important):

<html>
<body>
...
<form name="uploadForm" method="post" enctype="multipart/form-data"
action="(...my action...)">
...
Attachment 1: <input type="file" name="uploadedFiles"/>
Attachment 2: <input type="file" name="uploadedFiles"/>
Attachment 3: <input type="file" name="uploadedFiles"/>
...
</form>
...
</body>
</html>

Everytime I submit the form I obtain this exception:

javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1254)

org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.j
ava:821)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254)

org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)



java.lang.IllegalArgumentException: argument type mismatch
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39
)

sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl
.java:25)
java.lang.reflect.Method.invoke(Method.java:324)

org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.j
ava:1789)

org.apache.commons.beanutils.PropertyUtils.setNestedProperty(PropertyUtils.j
ava:1684)

org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:17
13)

org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:1019)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:1252)

org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.j
ava:821)

org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:254)

org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:525)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)

If I adjust the java source to manage one file only (ie. FileForm
instead of FileForm[]) it works, but as I try to use an array I get the
Exception.

I use struts 1.1 on Windows XP platform and Tomcat 5.0.24.
I've seen your post about this subject on the jakarta struts-user-list
and I was wondering if you ever had a chance to solve a similar problem.

Is it a bug of commons-beanutils or there's something wrong with my
code?

Thanks in advance,
Paul

Loading...