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

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

public FormFile  getPic() {
return pic;

public void setPic(FormFile  pic) {
this.pic = pic;
}
}
Action
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)

java.lang.IllegalArgumentException: Cannot invoke com.yourcompany.struts.form.PicForm.setPic - 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: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)

解决办法
jsp页面:
<html:form action="/pic.do" enctype="multipart/form-data"  method="post">
<html:file property="pic"></html:file>
<html:submit value="上传"></html:submit>
</html:form>

你可能感兴趣的:(java,apache,tomcat,struts,servlet)