Struts 2文件上传:文件对象为空

时间:2013-12-22 20:07:44

标签: java file-upload struts2

我正在尝试使用Struts 2文件上传,但在我看来它不起作用。以下是我的代码。

UploadAction.java

public class UploadAction extends ActionSupport{

    private File file;
    private String orgFileName;
    private String orgContentType;

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

    public void setUploadContentType(String contentType){
        this.orgContentType=contentType;
    }
    public void setUploadFileName(String fileName){
        this.orgFileName=fileName;
    }

    @Override
    public String execute(){
        if(file==null)
        {
            System.out.println("No file....");
        }
        System.out.println(orgContentType);
        System.out.println(orgFileName);
        return SUCCESS;
    }


}

struts.xml

    <constant name="struts.multipart.maxSize" value="20971520" />
    <constant name="struts2.multipart.saveDir" value="C:/users/sabertooth/desktop/upload" />
    <include file="example.xml"/>

    <!-- Configuration for the default package. -->
    <package name="default" extends="struts-default">
        <action name="upload" class="UploadAction">

            <result name="success">/example/HelloWorld.jsp</result>
        </action>
    </package>

我也在尝试设置struts2.multipart.saveDir属性,如上所示,但是当我读取服务器日志时,我看到这一行

unable to find `struts.multipart.saveDir` defaulting to `javax.servlet.dir`

此外,文件对象为null,因为没有文件...在控制台上打印出来。 我无法弄清楚这里有什么问题。

编辑:

fileupload.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>upload file below</h1>
        <s:form action="upload" method="post" enctype="multipart/form-data">
            <input type="file" name="file" id="uploadfile" />
            <input type="submit" id="submit" />
        </s:form>

    </body>
</html>

2 个答案:

答案 0 :(得分:4)

除了更改saveDir(真的没有必要和危险)之外,你没有遵循Action类中的约定:私有变量的名称必须与其Getter和Setters的名称相匹配;最后,在页面中,您通过指向私有变量而不是setter来匹配名称。将其更改为:

public class UploadAction extends ActionSupport{

    private File   upload;
    private String uploadFileName;
    private String uploadContentType;

    public void setUpload(File upload){
        this.upload=upload;
    }
    public void setUploadContentType(String uploadContentType){
        this.uploadContentType=uploadContentType;
    }
    public void setUploadFileName(String uploadFileName){
        this.uploadFileName=uploadFileName;
    }

    @Override
    public String execute(){
        if(upload==null)
        {
            System.out.println("No file....");
        }
        System.out.println(uploadContentType);
        System.out.println(uploadFileName);
        return SUCCESS;
    }
}

JSP

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

答案 1 :(得分:2)

更改此

        <input type="file" name="file" id="uploadfile" />

        <input type="file" name="upload" id="uploadfile" />

您的操作类中的setter是setUpload,因此它正在查找名为upload而不是file的请求参数。为了惯例,您还应该更改

private File file;

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

private File upload;

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