2010-7-11 13:44:32 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Struts has detected a file upload UI tag (s:file) being used without a form set to enctype 'multipart/form-data'. This is probably an error!
2010-7-11 13:44:32 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Struts has detected a file upload UI tag (s:file) being used without a form set to method 'POST'. This is probably an error!
2010-7-11 13:44:44 com.opensymphony.xwork2.util.logging.commons.CommonsLogger warn
警告: Error setting expression 'file' with value '[Ljava.lang.String;@19f410'
ognl.MethodFailedException: Method "setFile" failed for object com.action.UploadAction@1f87254 [java.lang.NoSuchMethodException: com.action.UploadAction.setFile([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:1265)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:1454)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:85)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:162)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
at com.opensymphony.xwork2.ognl.accessor.CompoundRootAccessor.setProperty(CompoundRootAccessor.java:65)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:2225)
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:198)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:161)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:149)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:276)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:187)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)

解决方案 »

  1.   

    <?xml version="1.0" encoding="UTF-8" ?> 
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts> <constant name="struts.custom.il8n.resources" value="message"></constant> <!-- 定义国际化方式,将struts2内置的验证提示方式覆盖掉 “message”为自己创建的文件,且必须在src根目录下 -->
    <constant name="struts.i18n.encoding" value="gbk"></constant>
    <constant name="struts.multipart.saveDir" value="f:"></constant> <package name="struts2" extends="struts-default">

    <interceptors>   <!-- 拦截器根节点 -->
    <interceptor name="auth" class="interceptor.AuthInterceptor"></interceptor>  <!-- 定义一个拦截器 -->

    <interceptor name="MethodFilterInterceptor" class="interceptor.MethodFilterInteceptor"></interceptor> <!-- 对指定方法进行拦截 -->


    <interceptor-stack name="default">    <!-- 定义一个拦截器栈 -->
    <interceptor-ref name="auth"></interceptor-ref>
    <interceptor-ref name="defaultStack"></interceptor-ref>  <!-- 引用继承下来的默认拦截器 -->
    </interceptor-stack>


    </interceptors>



    <!-- <default-interceptor-ref name="default">  --> <!-- 定义一个默认的拦截器引用 -->

    <!--</default-interceptor-ref>-->

    <global-results> <!-- 定义一个全局的结果 -->
    <result name="login" type="redirect">/login.jsp</result>  
    </global-results>

    <action name="login" class="com.action.LoginAction">
    <result name="success">/register2.jsp</result>
    <result name="input">/login.jsp</result>
    <result name="false">/login.jsp</result>
    </action>
    <action name="pointConvert" class="com.action.PointAction">
    <result name="success">/output.jsp</result>
    </action>
    <action name="register" class="com.action.RegisterAction" method="reg">   <!-- Action会调用“reg”这个方法 -->
    <result name="success">/success.jsp</result>
    <result name="input">/register2.jsp</result>

    <interceptor-ref name="MethodFilterInterceptor">
    <param name="includeMethods">text,execute</param> <!-- 对指定方法进行拦截 -->
    <param name="excludeMethods">execute</param> <!-- 对指定方法不拦截 -->
    </interceptor-ref>

    <interceptor-ref name="auth"></interceptor-ref>
    <interceptor-ref name="defaultStack"></interceptor-ref>  <!-- 引用继承下来的默认拦截器 -->

    <!-- 
    <interceptor-ref name="auth"></interceptor-ref>  
    <interceptor-ref name="defaultStack"></interceptor-ref>
     -->

    </action>

    <action name="upload" class="com.action.UploadAction">
    <result name="success">/uploadfile/result.jsp</result>
    </action>
    </package>
    </struts>
      

  2.   

    UploadActionpackage com.action;import java.io.*;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class UploadAction extends ActionSupport {
    private String username;
    private File file;
    private String fileFileName;
    private String fileContentType;
    public String getUsername() {
    return username;
    }
    public void setUsername(String username) {
    this.username = username;
    }
    public File getFile() {
    return file;
    }
    public void setFile(File file) {
    this.file = file;
    }
    public String getFileFileName() {
    return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
    this.fileFileName = fileFileName;
    }

    public String getFileContentType() {
    return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
    this.fileContentType = fileContentType;
    }
    @Override
    public String execute() throws Exception {
    InputStream is = new FileInputStream(file);
    String path = ServletActionContext.getRequest().getRealPath("/uploadfile");
    OutputStream os = new FileOutputStream(new File(path,this.getFileFileName()));
    byte[] bu = new byte[1024];
    int length = 0;
    while((length=is.read())>0){
    os.write(bu, 0, length);
    }
    is.close();
    os.close();

    return SUCCESS;
    }

    }
      

  3.   

    upload.jsp<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%@ taglib uri="/struts-tags" prefix="s" %><%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'upload.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->  </head>
      
      <body>
        <s:form action="upload">
         <s:textfield name="username" label="username"></s:textfield><br>
         <s:file name="file" label="file"></s:file><br>
         <s:submit></s:submit>
        </s:form>
      </body>
    </html>
      

  4.   

     without a form set to enctype 'multipart/form-data'.要设置form的enctype。
      

  5.   

     <s:form action="admin/addImageItems.do" method="post" enctype="multipart/form-data">
    加上红色字体那部分
      

  6.   

    错误消息提示的很明显:

    警告: Struts has detected a file upload UI tag (s:file) being used without a form set to enctype 'multipart/form-data'. This is probably an error!

    就是form没有设置 enctype="multipart/form-data"