• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File Upload using Struts

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to the Struts forum.
 
Nagesh Rachakonda
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any inpts are welcome.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the same error when I didn't have a FormFile setter method.

public void setFile(FormFile file) {
this.file = file;
}
 
Ranch Hand
Posts: 415
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
<html:form action="/upload" enctype="multipart/form-data">

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

Spent hours figuring this out.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
reply
    Bookmark Topic Watch Topic
  • New Topic