日期:2014-05-17  浏览次数:20753 次

求大神照顾,Struts2文件上传的问题
JSP:

        <form action="upFile" method="post" enctype="mutipart/form-data">
            文件:<input name="image" type="file" />
            <input type="submit" value="上传"/>
        </form>


Action:

public class UpFileAction {
private File image;
private String imageFileName;
        .
        .
        //getter/setter方法
        .
        .
public String execute() throws Exception {
String realPath = ServletActionContext.getServletContext().getRealPath("/images");
System.out.println(realPath);
File file = new File(new File(realPath),imageFileName);
if(!file.exists()) file.getParentFile().mkdirs();
FileUtils.copyFile(image, file);
ServletActionContext.getContext().put("msg", "文件上传成功");
return Action.SUCCESS;
}
 
}


struts.xml

<package name="param" extends="struts-default">
  <action name="upFile" class="cn.struts.action.UpFileAction">
     <result name="success">/index.jsp</result>
  </action>
</package>


报如下异常:
警告: Error setting expression 'image' with value '[Ljava.lang.String;@575b2f17'
ognl.MethodFailedException: Method "setImage" failed for object cn.struts.action.LoginAction@58a1918e [java.lang.NoSuchMethodException: cn.struts.action.LoginAction.setImage([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1289)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1478)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:27)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2315)
at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:78)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2315)
at ognl.ASTProperty.setValueBody(ASTProperty.java:127)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:220)
at ognl.SimpleNode.setValue(SimpleNode.java:301)
at ognl.Ognl.setValue(Ognl.java:737)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:218)
at com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:187)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:174)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setParameter(OgnlValueStack.java:152)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:318)
at com.opens