• 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

struts file uploading problem

 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im having problem with the file uploading codes. Im using struts and hibernate and mysql. the errors and code is pasted below. please advice. thanks

Error Message:
java.lang.IllegalArgumentException: Cannot invoke com.strutsexample.form.ContentForm.setFile - argument type mismatch
org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1778)
org.apache.commons.beanutils.PropertyUtilsBean.setSimpleProperty(PropertyUtilsBean.java:1759)
org.apache.commons.beanutils.PropertyUtilsBean.setNestedProperty(PropertyUtilsBean.java:1648)
org.apache.commons.beanutils.PropertyUtilsBean.setProperty(PropertyUtilsBean.java:1677)
org.apache.commons.beanutils.BeanUtilsBean.setProperty(BeanUtilsBean.java:1022)
org.apache.commons.beanutils.BeanUtilsBean.populate(BeanUtilsBean.java:811)
org.apache.commons.beanutils.BeanUtils.populate(BeanUtils.java:298)
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:451)
org.apache.struts.chain.commands.servlet.PopulateActionForm.populate(PopulateActionForm.java:45)
org.apache.struts.chain.commands.AbstractPopulateActionForm.execute(AbstractPopulateActionForm.java:57)
org.apache.struts.chain.commands.ActionCommandBase.execute(ActionCommandBase.java:48)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.commons.chain.generic.LookupCommand.execute(LookupCommand.java:304)
org.apache.commons.chain.impl.ChainBase.execute(ChainBase.java:190)
org.apache.struts.chain.ComposableRequestProcessor.process(ComposableRequestProcessor.java:280)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1858)
org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:446)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)



action class code:
ContentForm cfrm = (ContentForm) form;
HibernateSessionFactory.getSessionFactory().getCurrentSession().beginTransaction();
Content content=new Content();
content.setFile(Hibernate.createBlob(cfrm.getFile().getInputStream()));
HibernateSessionFactory.getSessionFactory().getCurrentSession().save(content);


formbean code:
private FormFile file;
public FormFile getFile() {
return file;
}

public void setFile(FormFile file) {
this.file = file;
}


object class:
private Blob file;

public Blob getFile() {
return file;
}

public void setFile(Blob file) {
this.file = file;
}
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's hard to tell, but it looks like you've created a custom class named FormFile, and the code for that object is displayed above under the heading "object class". Is that right?

If so, this is what is causing the problem. When doing a file upload, the ActionForm property associated with the file to be uploaded must be of type org.apache.struts.upload.FormFile. It can't be a custom class.

Struts will populate this object for you when the form is submitted. In your Action class, you can then use the FormFile's getInputStream() method to get an input stream for the file being uploaded. Once you have the input stream, you can then use it to write to another object such as a file or a Blob.

This link shows an example of both a file upload to a Blob and a file download from a Blob.
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to set the inputstream to an object. what datatype should i declare?
i have declare the file as

private byte[] file;

but i got the following error:
setFile(byte[]) in com.strutsexample.model.Content cannot be applied to (java.io.InputStream)
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The file property of your Content bean should be of type java.sql.Blob, and you should use the original code that you showed us to populate it:

content.setFile(Hibernate.createBlob(cfrm.getFile().getInputStream()));

 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
im having the following error using the codings displayed below:
java.lang.NullPointerException
com.strutsexample.action.ContentAction.insert(ContentAction.java:51)
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:585)


This is my object class


this is my formbean class:


my struts action class:
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The Stack trace tells you that a NullPointerException is occuring at line 51 of your ContentAction class. Find which object is being dereferenced at line 51. That's the object that is null when you aren't expecting it to be. Then find out why it's null.
 
lynn fann
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is the line:
content.setFile(Hibernate.createBlob (myFile.getInputStream()));
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
myFile must be null. Find out why.

Here's one thing you may want to check: If the size of the file is greater than what you specified in your struts-config.xml file in <controller maxFileSize="" /> Struts will not upload the file and the property in your ActionForm will be null.
[ March 12, 2007: Message edited by: Merrill Higginson ]
reply
    Bookmark Topic Watch Topic
  • New Topic