首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

struts2文件下传报错

2012-12-22 
struts2文件上传报错Caused by: java.lang.NoSuchMethodException: xhy.shjc.UserAction.setUploadFile([L

struts2文件上传报错
Caused by: java.lang.NoSuchMethodException: xhy.shjc.UserAction.setUploadFile([Ljava.lang.String;)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1230)
... 63 more
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: xhy.shjc.UserAction.setUploadFile([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:77)
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:217)
at com.opensymphony.xwork2.ognl.OgnlValueStack.trySetValue(OgnlValueStack.java:186)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:173)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setParameter(OgnlValueStack.java:151)
。。。。

Action的代码是

package xhy.shjc;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;

public class UserAction{
//public User user;
private File uploadFile;
private String fileName;

public String add(){ 
try {
FileUtils.copyFile(uploadFile, new File("g:/Temp/"+fileName));
} catch (IOException e) {
e.printStackTrace();
}

System.out.println(fileName);
return "success";
}

public File getUploadFile() {
return uploadFile;
}
public void setUploadFile(File uploadFile) {
this.uploadFile = uploadFile;
}
public void setUploadFileFileName(String fName) {
fileName = fName;
}
public void setUploadFileContentType(String ct){
System.out.println(ct);
}

struts.xml:
<action name="test" class="xhy.shjc.UserAction">
      <!-- <interceptor-ref name="defaultStack"></interceptor-ref>  -->
            <result name="success">
                /success.jsp
            </result>
        </action>  

jsp:
<form action="test" method="post" enctype="multipart/formdata">
<input type="hidden" name="method:add">
<input type="file" name="uploadFile">
<input type="submit" name="submit" value="上传文件">
<s:debug></s:debug>
  </form>
[最优解释]
enctype="multipart/formdata"错了应该是enctype="multipart/form-data



[其他解释]
搜索了一篇文章,希望对你有用。
http://www.2cto.com/kf/201202/118589.html
[其他解释]
在struts.xml中对应的action配置
 <interceptor-ref name="fileUpload">
  <param name="allowedTypes">text/html,text/plain</param>
             <param name="maximumSize">2048000</param>
       </interceptor-ref>
    <interceptor-ref name="defaultStack"/>
[其他解释]
你的方法是setUploadFile(File uploadFile)而你真真调用的是setUploadFile(String ..)方法,所以报找不到方法错!

热点排行