读书人

求大神照顾Struts2文件下传的有关问

发布时间: 2013-01-12 16:25:03 作者: rapoo

求大神照顾,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.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:230)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:98)
.....
部分异常省略
.....
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)


at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.NoSuchMethodException: cn.struts.action.LoginAction.setImage([Ljava.lang.String;)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1230)
... 67 more
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: cn.struts.action.LoginAction.setImage([Ljava.lang.String;)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1230)
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 java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
\--------------------------------------/
C:\Apache\Tomcat 7.0\webapps\Struts\images
2012-12-29 23:16:15 com.opensymphony.xwork2.util.logging.commons.CommonsLogger error
严重: Exception occurred during processing request: null
java.lang.NullPointerException
at java.io.File.<init>(File.java:305)
at cn.struts.action.LoginAction.add(LoginAction.java:39)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:446)
at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:285)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:248)
at org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:256)
.....
部分异常省略
.....
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619) struts file

action
[解决办法]
将image变量改其它名字试试,例如:imageFile
[解决办法]
求大神照顾,Struts2文件下传的有关问题

读书人网 >Java Web开发

热点推荐