1

How can I get original filename of an uploaded file in Struts2. Currently what I am getting is a filename.tmp filename whereas the file being uploaded is a CSV and the filename is different to what I am getting. Here is the JSP code

Select file Change Remove

Here is the action mapping in struts config

        <action name="survey/send" class="surveyAction" method="send">
            <interceptor-ref name="authorizationStack"/>
            <interceptor-ref name="fileUpload">
                <param name="maximumSize">20971520</param>
            </interceptor-ref>
            <result name="success" type="tiles">orgAdmin.survey.send.settings</result>
            <result name="input" type="tiles">orgAdmin.survey.send.settings</result>
            <result name="error" type="tiles">orgAdmin.survey.send.settings</result>
        </action>

Action class contains the following 3 instance variables instance with getter and setter

    public File upload;
    private String contentType;
    private String filename;

However, upload.getName() is not getting the actual filename, whereas contentType and filename properties are null.

Roman C
  • 49,761
  • 33
  • 66
  • 176

1 Answers1

0

The naming convention for the setters is

setX(File file)
setXContentType(String contentType)
setXFileName(String fileName)

where X corresponds to the value of the name attribute of the <s:file /> tag in your jsp page

Jsp form

<s:form action="doUpload" method="post" enctype="multipart/form-data">
   <s:file name="upload" label="File"/>
   <s:submit/>
</s:form>

Action class

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

// Getters and setters
// ActionSupport methods 
Harish
  • 16