struts 上传文件时javax.servlet.ServletException: BeanUtils.populate解决办法

[color=red]jsp页面:[/color]
<html:form action="/pic.do" method="post">
<html:file property="pic"></html:file>
<html:submit value="上传"></html:submit>
</html:form>
[color=red]formbean[/color]
public class PicForm extends ActionForm {
private FormFile pic;

public FormFile getPic() {
return pic;

public void setPic(FormFile pic) {
this.pic = pic;
}
}
[color=red]Action [/color]
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException {
PicForm picForm = (PicForm) form;// TODO Auto-generated method stub
FormFile file =picForm.getPic();
String photoPath = this.getServlet().getServletContext().getRealPath("/") + "upload\\" + file.getFileName();//存储目录,可以在配置文件里设置,这样可以方便更改存储目录
System.out.print(photoPath);
FileOutputStream fos;
try {
fos = new FileOutputStream(photoPath);
fos.write(file.getFileData());
fos.flush();
fos.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


return null;
}


报错误
javax.servlet.ServletException: BeanUtils.populate
org.apache.struts.util.RequestUtils.populate(RequestUtils.java:495)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:816)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

[color=red]java.lang.IllegalArgumentException: Cannot invoke com.yourcompany.struts.form.PicForm.setPic - argument type mismatch[/color]
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:493)
org.apache.struts.action.RequestProcessor.processPopulate(RequestProcessor.java:816)
org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:203)
org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)
org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

解决办法
[color=red]jsp页面:[/color]
<html:form action="/pic.do" [color=red]enctype="multipart/form-data"[/color] method="post">
<html:file property="pic"></html:file>
<html:submit value="上传"></html:submit>
</html:form>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值