Struts 2 file upload : File object being null

2019-01-27 04:46发布

I am trying to use Struts 2 file upload but it seems to me that its not working. Below is my code.

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>

I am also trying to set struts2.multipart.saveDir property as you can see above but when I read the server logs I see this line

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

Also the file object is null as no file... gets printed out on console. I can't figure out what is wrong here.

EDIT:

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条回答
干净又极端
2楼-- · 2019-01-27 05:28

Change this

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

to

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

Your setter in your action class is setUpload so it is looking for a request parameter called upload, not file. For the sake of convention you should also change

private File file;

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

to

private File upload;

public void setUpload(File file){
    this.upload=file;
}
查看更多
我只想做你的唯一
3楼-- · 2019-01-27 05:35

Apart from changing the saveDir (really not necessary, and dangerous), your are not following the conventions in the Action class: the name of an private variable must match the names of its Getters and Setters; and finally, in page you are mismatching the name by pointing to the private variable, not the setter. Change it to:

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>
查看更多
登录 后发表回答