wood burning stoves 2.0*
The moose likes Struts and the fly likes File Upload using Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "File Upload using Struts" Watch "File Upload using Struts" New topic
Author

File Upload using Struts

Nagesh Rachakonda
Ranch Hand

Joined: Dec 04, 2003
Posts: 34
Hi all,
While trying to upload a file using struts, I get the following exception even before I hit my action class. I have two file tags on my jsp but even when I use one(comment the other out) , I get the same exception. I have tried submitting the form with and without choosing a file, and its still the same..... Is this a known bug in Struts? Does anybody have a fix to this?
[3/4/04 18:18:05:935 EST] 269187a1 WebGroup X Servlet Error:
BeanUtils.populate: java.lang.IllegalArgumentException: argument type mismatch
at java.lang.reflect.Method.invoke(Native Method)
at org.apache.commons.beanutils.PropertyUtils.setSimpleProperty(PropertyUtils.j
ava:1650)
at org.apache.commons.beanutils.PropertyUtils.setNestedProperty(PropertyUtils.j
ava:1545)
at org.apache.commons.beanutils.PropertyUtils.setProperty(PropertyUtils.java:15
74)
at
org.apache.commons.beanutils.BeanUtils.setProperty(BeanUtils.java:919)
at
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:726)
at
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:978)
at org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.j
ava:779)
at
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:246)
at
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1292)
at
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:510)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:760)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)
at com.ibm.servlet.engine.webapp.StrictServletInstance.doService(ServletManager
.java:827)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet._service(StrictLifecycl
eServlet.java:167)
at com.ibm.servlet.engine.webapp.IdleServletState.service(StrictLifecycleServle
t.java:297)
at com.ibm.servlet.engine.webapp.StrictLifecycleServlet.service(StrictLifecycle
Servlet.java:110)
at com.ibm.servlet.engine.webapp.ServletInstance.service(ServletManager.java:47
2)
at com.ibm.servlet.engine.webapp.ValidServletReferenceState.dispatch(ServletMan
ager.java:1012)
at com.ibm.servlet.engine.webapp.ServletInstanceReference.dispatch(ServletManag
er.java:913)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.handleWebAppDispatch(W
ebAppRequestDispatcher.java:678)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.dispatch(WebAppRequest
Dispatcher.java:331)
at com.ibm.servlet.engine.webapp.WebAppRequestDispatcher.forward(WebAppRequestD
ispatcher.java:117)
at
com.ibm.servlet.engine.srt.WebAppInvoker.doForward(WebAppInvoker.java:134)
at com.ibm.servlet.engine.srt.WebAppInvoker.handleInvocationHook(WebAppInvoker.
java:239)
at com.ibm.servlet.engine.invocation.CachedInvocation.handleInvocation(CachedIn
vocation.java:67)
at com.ibm.servlet.engine.invocation.CacheableInvocationContext.invoke(Cacheabl
eInvocationContext.java:106)
at com.ibm.servlet.engine.srp.ServletRequestProcessor.dispatchByURI(ServletRequ
estProcessor.java:154)
at com.ibm.servlet.engine.oselistener.OSEListenerDispatcher.service(OSEListener
.java:315)
at com.ibm.servlet.engine.http11.HttpConnection.handleRequest(HttpConnection.ja
va:60)
at
com.ibm.ws.http.HttpConnection.readAndHandleRequest(HttpConnection.java:323)
at com.ibm.ws.http.HttpConnection.run(HttpConnection.java:252)
at com.ibm.ws.util.CachedThread.run(ThreadPool.java:137)
May I know when I will get such kinds of exceptions and problems.
Thanks for your time.
Nagesh.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60041
    
  65

Moving to the Struts forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nagesh Rachakonda
Ranch Hand

Joined: Dec 04, 2003
Posts: 34
Any inpts are welcome.
Jamie Grant
Greenhorn

Joined: Aug 25, 2004
Posts: 2
I got the same error when I didn't have a FormFile setter method.

public void setFile(FormFile file) {
this.file = file;
}
sreenath reddy
Ranch Hand

Joined: Sep 21, 2003
Posts: 415
Hi Nagesh

Ya the above exception is because the set and get methods for that file should be of type of FormFile as mentioned above ..since struts uses refelection to populate the formbean and if it finds <htm:file > in ur jsp it will look for method of type FormFile .......so thats giving some exception
Rick Herrick
Greenhorn

Joined: Aug 25, 2004
Posts: 11
I'm getting this same error, but I have (what I believe to be) the proper getter and setter methods for the file uploads. I have this in my JSP page:



(with all the other necessary stuff; this page is currently working except for the upload section).

Then in my form bean, I have:



I then get the exception:



If I comment out the file controls, I have no problems submitting the form. With the file controls, I can't submit the form, even when I'm not uploading a file.

Any ideas would be great! Argument type mismatch? What argument? What types? This info would be helpful


Rick Herrick<br />C#/.NET, Java, Ruby, Agile as hell
Rick Herrick
Greenhorn

Joined: Aug 25, 2004
Posts: 11
OK, thus far, I've answered one of my own questions before anyone else and here's another

This is a vexing problem with a really simple solution! Make sure that you set the enctype properly in your html:form element:



That did it for me.
James Qi
Greenhorn

Joined: Nov 29, 2004
Posts: 1
Hi,

I have tried this upload thing many times, and I am also getting the same error. I think I have included everything, the form tag in JSP, the FormBean implemenation... I just do not know what I have not put in! It is still throwing the same error to me!

Could be a problem with Struts on Weblogic? Or something else? Can you use html:file with other input fields?

Please help!
vjdev vjdev
Greenhorn

Joined: Nov 29, 2005
Posts: 1
<html:form action="/upload" enctype="multipart/form-data">

Make sure the enctype="---" occurs before the post="---" in the form

Spent hours figuring this out.
Roy R.
Greenhorn

Joined: Feb 19, 2006
Posts: 2
In the method shown to upload a file in struts,how shld i knw where the uploaded file is being saved in the server?
Can nyone paste a code of setting the path of saving a file to be uploaded in Struts using Form File?
PLz reply at earliest.
Roja Rani
Greenhorn

Joined: Sep 28, 2005
Posts: 22
Originally posted by vjdev vjdev:
<html:form action="/upload" enctype="multipart/form-data">

Make sure the enctype="---" occurs before the post="---" in the form

Spent hours figuring this out.


Hi,

Thank you for the suggestion.. It solved my problem.

Regards,
Roja.
Tal Nathan Tal Nathan
Greenhorn

Joined: Feb 26, 2006
Posts: 13
Note: I am very new to struts so there may very well be a simpler/better way
to do this, but this method does work for getting an uploaded file and saving it.

In your action class do the following:

FormFile myFile = myForm.getTheFile();
String fileName = myFile.getFileName();
InputStream input = null;
input = myFile.getInputStream();
DataInputStream dis = new DataInputStream(new BufferedInputStream(input));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(saveLocation));
int i = 0;
while (((i = dis.read()) != -1)) {
out.write(i);
}
out.flush();
out.close();

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tal Nathan Tal Nathan
Greenhorn

Joined: Feb 26, 2006
Posts: 13
Come to think of it, the above method is not every efficient.
Rather create byte array [1024] and use that to read the data in order to make sure you read 1kb at a time rather then 1 byte at a time.
Abundio Adar
Greenhorn

Joined: Feb 28, 2006
Posts: 1
Hi to all!

I have used this process (enctype="multipart/form-data") to upload the file.
I'm using Japanese Characters. Problem is, my characters becomes "garbage" when I'm clicking the submit button.
I've seen a solution but it still does'nt help. http://www.venkks.net/2005/03/unicode-or-non-iso-8859-1-encoding-and.html

Any ideas?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File Upload using Struts
 
Similar Threads
File upload & exception handling - major bug?
FileUploading in struts
Getting Exception
Websphere, file upload error: "Read timed out"
Multiple file upload