aspose file tools*
The moose likes Struts and the fly likes struts file uploading problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "struts file uploading problem" Watch "struts file uploading problem" New topic
Author

struts file uploading problem

lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
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;
}
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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.


Merrill
Consultant, Sima Solutions
lynn fann
Ranch Hand

Joined: Oct 15, 2006
Posts: 115
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Oct 15, 2006
Posts: 115
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

Joined: Feb 15, 2005
Posts: 4864
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

Joined: Oct 15, 2006
Posts: 115
this is the line:
content.setFile(Hibernate.createBlob (myFile.getInputStream()));
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
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 ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts file uploading problem