解决struts多文件上传出现的问题

This topic has 9 replies on 1 page.   
 

Multiple form upload
Author: pperliti  Posts: 5   Registered: 4/29/02   Jul 19, 2004 12:21 PM   
 

Hi everybody.
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):


...
<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>
...

 

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.java: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.java:1789)
org.apache.commons.beanutils.PropertyUtils.setNestedProperty(PropertyUtils.java:1684)
org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:1713)
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.java: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.
Have you got any ideas? Is it a bug of commons-beanutils?
Thanks in advance,
Paul
 

Re: Multiple form upload
Author: Spit  Posts: 1   Registered: 4/20/05   Apr 20, 2005 7:40 AM (reply 1 of 9)   
 

Hello, I have the same problem, if you discovery any solution please email me.
Thanks, Sim&atilde;o Castro
 

Re: Multiple form upload
Author: vbatista  Posts: 11   Registered: 5/29/98   Apr 21, 2005 2:35 AM (reply 2 of 9)   
 

Hi!
I am having the same error. I need to upload N files, being N dynamic between requests.
Have you solved your problem?
Any hint would be appreciated.

Regards,
Victor Batista
 

Re: Multiple form upload
Author: mshanu  Posts: 488   Registered: 3/24/05   Apr 21, 2005 3:57 AM (reply 3 of 9)   
 

Attachment 1: <input type="file" name="uploadedFiles"/>
Attachment 2: <input type="file" name="uploadedFiles"/>
Attachment 3: <input type="file" name="uploadedFiles"/>
give unique names to the name of the file fields..

regards
shanu
 

Re: Multiple form upload
Author: vbatista  Posts: 11   Registered: 5/29/98   Apr 21, 2005 5:16 AM (reply 4 of 9)   
 

Hi!
Thks for your reply.
If I define mutiple file inputs :

<input type="file" name="uploadFile" ...
><input type="file" name="uploadFile" ...
...

and define a variable on the Struts form like;
private FormFile[] uploadFile = null;

Struts throws the exception below while trying to fill the array.
I can fill arrays of Strings, ints,....but with the FormFile I am having this Exception. I would prefer to use Struts mechanism to upload files instead of doing things "manually". Any help would be appreciated.

Thanks in advance,
Victor Batista


13:07:38,603 ERROR [Engine] StandardWrapperValve[struts]: Servlet.service() for
servlet struts threw exception
java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(Property
Utils.java:1789)
at org.apache.commons.beanutils.PropertyUtils.setNestedProperty(Property
Utils.java:1684)
at org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.
java:1713)
at org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:101
9)
at org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:808)
at org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
at org.apache.struts.action.RequestProcessor.processPopulate(RequestProc
essor.java:798)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.ja
va:205)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:116
4)
at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at pt.ptinovacao.dino.admin.servlet.filter.StrutsFilter.doFilter(StrutsF
ilter.java:60)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at pt.ptinovacao.dino.admin.servlet.filter.DefaultFilter.doFilter(Defaul
tFilter.java:55)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
lter.java:75)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:186)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(Standard
ContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrinc
ipalValve.java:66)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
yAssociationValve.java:150)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(Authentica
torBase.java:462)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
e.java:54)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:137)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:118)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:
535)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:102)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:109)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValv
eContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.jav
a:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)

at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:16
0)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java
:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadP
ool.java:683)
at java.lang.Thread.run(Thread.java:534)>
 

Re: Multiple form upload
Author: linxpda  Posts: 651   Registered: 12/26/04   Apr 21, 2005 10:12 AM (reply 5 of 9)   
 

Instead of this:

Attachment 1: <input type="file" name="uploadedFiles"/>
Attachment 2: <input type="file" name="uploadedFiles"/>
Attachment 3: <input type="file" name="uploadedFiles"/>

use this and try it:

Attachment 1: <input type="file" name="attachment1"/>
Attachment 2: <input type="file" name="attachment2"/>
Attachment 3: <input type="file" name="attachment3"/>

You're uploading three files, all with the same request parameter name. Heck, that would cause me to bust a blood vessel.
 


Re: Multiple form upload
Author: romano_nj  Posts: 1   Registered: 6/16/05   Jun 16, 2005 12:29 PM (reply 9 of 9)   
 
======================= 从这里开始 ==============================
I've been battling this for some time now and here is how you get around it:

Jsp page:
<html:file property='<%="referenceFormFile["+i+"]"%>' size="40"/>
(注意是带[])
where i is some counter generated by for loop or iterator

Action form:
Create property -> private FormFile[] referenceFormFile;

Add the following methods:
public FormFile[] getReferenceFormFile()
{
return referenceFormFile;
}

public void setReferenceFormFile(FormFile[] referenceFormFile)
{
this.referenceFormFile = referenceFormFile;
}

public FormFile getReference(int index) 

return referenceFormFile[index];
}

public void setReference(int index,FormFile reference) 

referenceFormFile[index] = reference;
}

Make sure to initialize array within reset method and on load

I did it this way:
/**
* initialize array of file upload
* @param request
*/
public void initArray(HttpServletRequest request)
{
referenceFormFile = new FormFile[MAX_NUMBER_REFERENCES];
}

Make sure you call it from reset method and when action form is created

Hope it helps,
Sincerely Roman
================== 到这里结束 ================================



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值