文件上传拦截器不会调用setter方法

时间:2014-05-20 18:44:16

标签: java file-upload struts2

我需要做非常基本的文件上传操作,但在我的情况下,文件上传拦截器不会调用setter方法。

我已经在stackoverflow上检查了类似问题like this的解决方案,但他们没有解决我的问题。

请让我知道我在代码中犯了什么错误。

动作类

public class ResultFileUploadAction extends ActionSupport {
    private File upload;
    private String uploadFileName;
    private String uploadContentType;
    private Logger logger = Logger.getRootLogger();

    @Override
    public String execute() throws Exception {
        logger.info("ResultFileUploadAction->execute");
        String destPath = "C:/work/";
        try {
            System.out.println("Src File name: " + upload);
            System.out.println("Dst File name: " + uploadFileName);
            File destFile = new File(destPath, uploadFileName);
            FileUtils.copyFile(upload, destFile);

        } catch (IOException e) {
            e.printStackTrace();
            return ERROR;
        }

        return super.execute();
    }

    public void setUpload(File upload) {
        this.upload = upload;
    }

    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }

    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }
}

Jsp文件

<body>

    <s:form action="upload" method="post" enctype="multipart/form-data">
        <input type="file" name="upload" id="uploadfile" />
        <input type="submit" id="submit" />
    </s:form>
</body>

struts.xml中

<interceptors>
    <interceptor name="fileupload"
        class="org.apache.struts2.interceptor.FileUploadInterceptor"></interceptor>
    <interceptor name="servletConfig"
        class="org.apache.struts2.interceptor.ServletConfigInterceptor" />
    <interceptor name="authenticationinterceptor"
        class="interceptors.common.AuthenticationInterceptor"></interceptor>
    <interceptor-stack name="securestack">
        <interceptor-ref name="authenticationinterceptor"></interceptor-ref>
        <interceptor-ref name="servletConfig"></interceptor-ref>

    </interceptor-stack>
</interceptors>

<action name="upload" class="actions.result.ResultFileUploadAction"
    method="execute">
    <interceptor-ref name="securestack"></interceptor-ref>
    <interceptor-ref name="fileupload"></interceptor-ref>
    <result name="success" type="dispatcher">/AddResultBullk.jsp</result>
</action>

因为没有调用setter因此我在NPE中获得了execute()

2 个答案:

答案 0 :(得分:1)

fileUpload拦截器文档中所述:

  

它添加了以下参数,其中[File Name]是HTML表单上传的文件的名称。 [fileName,contentType]

当使用拦截器堆栈进行混乱时,总会有两种攻击计划:

  • 尝试使用非自定义堆栈
  • 除非你确切知道为什么要这样做,以及你正在做什么,否则不要乱用堆栈。

此外,配置为几乎不使用拦截器的操作几乎总是可疑的,因为它们消除了大量的框架功能。特别是参数是基本上每一种基于形式的行动的关键。

答案 1 :(得分:1)

当你想要实现一个新的拦截器时,最好将它们添加到链的前面或后面的默认堆栈中,我在尝试添加拦截器进行身份验证之前遇到了同样的问题并结束了通过以下练习,我会更新你的 代码提到我的想法:

...
<interceptors>    
    <interceptor name="authenticationinterceptor" class="your.class.name" />
    <interceptor-stack name="securestack">
        <interceptor-ref name="authenticationinterceptor" />
        <interceptor-ref name="defaultStack" />
    </interceptor-stack>
</interceptors>
<!--
Implicitly use securestack for all actions.
-->
<default-interceptor-ref name="securestack" />
....
<!-- 
and if you want some actions to pass from secureStack and use defaultStack
(ex login page), you can state that explicitly, See:
-->
<action name="GetLoginPageAction" class="your.class.name">
    <interceptor-ref name="defaultStack" />
    ...
</action>

<!-- 
for upload action you can use interceptor without define 
it in the interceptors tag only in the action, 
Note that you should explicitly use securestack below
-->
<action name="upload" class="actions.result.ResultFileUploadAction">
    <!-- You can remove the below refs because fileUpload is already 
         included in defaultStack that is included in the securestack 
         and securestack is default interceptor for all actions.
    <interceptor-ref name="securestack"/> 
    <interceptor-ref name="fileUpload"/>
    -->
    ...
</action>

我希望这些笔记可以帮助你。