struts2文件upload-getting null文件名

时间:2015-08-19 19:51:29

标签: java-ee file-upload struts2 tomcat7

我试图上传文件,但每当我尝试上传文件并打印文件名时,我都会得到null并生成一个.tmp文件。我跟着mkong的方法here。我也在这个link尝试了SO的建议。不幸的是,我仍然陷入了这个问题,我想我犯了一个错误我一直在俯视。我正在使用tomcat7。  以下是我的文件:

JSP

<s:form id="login" name="login" method="post" action="uploadResume" enctype="multipart/form-data">
    <s:file name="uploadFile" label="Select a File to upload" size="40"/>
    <s:submit value="submit" name="submit" />
</s:form>

struts.xml中

    <constant name="struts.multipart.saveDir" value="/Users/shibasish/Documents/workspace/MSVenture/src/main/resources" />
<action name="uploadResume" class="com.msventure.web.actions.CompleteProfileAction" method="uploadResume">
            <interceptor-ref name="exception"/>
                <interceptor-ref name="i18n"/>
                <interceptor-ref name="fileUpload">
                <param name="allowedTypes">text/plain</param>
                <param name="maximumSize">10240</param>
            </interceptor-ref>
                <result name="success">/profile.jsp</result>
                <result name="fail">/login.jsp</result>
                <result name="index">/index.jsp</result>
                  <result name="login">/talent.jsp</result>
            </action>

行动的类

        private File uploadFile;
    private String uploadFileFileName;
    public String uploadResume(){
            try{
            String filePath = "/Users/shibasish/Documents";
            System.out.println("Server path:" + filePath);
            System.out.println("File Name:" + uploadFileFileName);
            File fileToCreate = new File(filePath, uploadFileFileName);          
            FileUtils.copyFile(uploadFile, fileToCreate);
            }catch(Exception e){}       
            return "success";
        }
public File getUploadFile() {
        return uploadFile;
    }

    public void setUploadFile(File uploadFile) {
        this.uploadFile = uploadFile;
    }

    public String getUploadFileFileName() {
        return uploadFileFileName;
    }

    public void setUploadFileFileName(String uploadFileFileName) {
        this.uploadFileFileName = uploadFileFileName;
    }

的pom.xml

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>
<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

请注意在课堂上设置了变异器。

1 个答案:

答案 0 :(得分:0)

我尝试使用更完整的拦截器堆栈,它运行正常。 我已将文件上传中的以下内容更改为 defaultStack拦截器: 的 struts.xml中

<action name="uploadResume" class="com.msventure.web.actions.CompleteProfileAction" method="uploadResume">
            <interceptor-ref name="defaultStack">
    <param name="fileUpload.allowedTypes">text/plain</param>
</interceptor-ref> 
        <result name="success">/profile.jsp</result>
        <result name="fail">/login.jsp</result>
        <result name="index">/index.jsp</result>
          <result name="login">/talent.jsp</result>
    </action>