Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

java - Struts 2 file upload : File object being null

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...