I'm not exactly sure what isn't working for you, but have you tried using the newer FilesUploadAware interface? More information can be found at https://struts.apache.org/core-developers/file-upload.html

I might also recommend you set <constant name="struts.devMode" value="true"/> to see if anything comes up in the logs.

Thanks,
Burton

------ Original Message ------
From "Zoran Avtarovski" <zo...@sparecreative.com>
To "Struts Users Mailing List" <user@struts.apache.org>
Date 2/28/2024 4:27:48 PM
Subject Struts 6.3 Issue Uploading Files

Hi Guys,



We are unable to upload files to our first 6.3 application using HTTP requests, 
but the strange thing is they work with ajax requests. I suspect we are 
overlooking something in the config which is required in 6.3.



We are using 6.3.0.2 running on Tomcat 9. The file object is null after the 
upload but we can see the upload is there.



I can see the params interceptor finds the file and copies it to the temp 
directory, but I then can’t access the file in the action??? What’s really 
strange is if we use the same mechanism using a an ajax request with FormData 
it works as expected. That’s why we created a simple setup as outlined below to 
identify the root cause.



Any help would really be appreciated.



Here’s a snippet of the logs:

multipart.JakartaMultiPartRequest (JakartaMultiPartRequest.java:103) - Found 
file item: [upload]

multipart.JakartaMultiPartRequest (JakartaMultiPartRequest.java:114) - Item is 
a file upload



When I try and access the file in my action:

action.IndexAction (IndexAction.java:54) - Uploaded file : null

action.IndexAction (IndexAction.java:55) - Uploaded file name : null

action.IndexAction (IndexAction.java:56) - Uploaded file type : null

action.IndexAction (IndexAction.java:57) - Uploaded file length  : 0





And then after the request has completed:

multipart.JakartaMultiPartRequest (JakartaMultiPartRequest.java:361) - Removing 
file upload name=Invoice - 1331-1.pdf, StoreLocation= 
/apache-tomcat-9.0.44_base/work/Catalina/localhost/caams/upload_0569c990_d32c_4688_ad50_44db275ab0cc_00000000.tmp,
 size=465396 bytes, isFormField=false, FieldName=upload



This is what my form looks like:



<s:form enctype="multipart/form-data" action="testUpload" method="post" 
theme="simple">

<s:file name="upload" key="file.to.upload" />

              <button type="submit" >Submit</button>

</s:form>



My action has the following (BaseAction extends ActionSupport):



public class IndexAction extends BaseAction {



    private File upload;

    private String uploadFileName;

    private String uploadContentType;

    private long uploadContentLength;



    public IndexAction() {

    }



    @Override

    public String execute() {

        return SUCCESS;

    }



    public String edit() {

        return INPUT;

    }



    public String uploadTest() {

        try {

            LOGGER.debug("Uploaded file : "+ upload);

            LOGGER.debug("Uploaded file name : "+ uploadFileName);

            LOGGER.debug("Uploaded file type : "+ uploadContentType);

            LOGGER.debug("Uploaded file length  : "+ uploadContentLength);



            inputStream = new FileInputStream(upload);



            if (inputStream != null) {

                inputStream.close();

            }



        } catch (Exception e) {

            LOGGER.error("General . uploading error :", e);

        }



        return SUCCESS;

    }



    public File getUpload() {

        return upload;

    }



    public void setUpload(File upload) {

        this.upload = upload;

    }



    public String getUploadFileName() {

        return uploadFileName;

    }



    public void setUploadFileName(String uploadFileName) {

        this.uploadFileName = uploadFileName;

    }



    public String getUploadContentType() {

        return uploadContentType;

    }



    public void setUploadContentType(String uploadContentType) {

        this.uploadContentType = uploadContentType;

    }



    public long getUploadContentLength() {

        return uploadContentLength;

    }



    public void setUploadContentLength(long uploadContentLength) {

        this.uploadContentLength = uploadContentLength;

    }

}



My relevant struts.xml config:



    <constant name="struts.enable.DynamicMethodInvocation" value="false"/>

    <constant name="struts.i18n.encoding" value="UTF-8"/>

    <constant name="struts.devMode" value="false"/>

    <constant name="struts.locale" value="en_AU"/>

    <constant name="struts.ui.theme" value="simple"/>

    <constant name="struts.ui.templateDir" value="template"/>



    <constant name="struts.i18n.search.defaultbundles.first" value="true"/>

    <constant name="struts.ui.escapeHtmlBody" value="true"/>



    <!-- Max file size of 15mb -->

    <constant name="struts.multipart.maxSize" value="45000000" />

    <constant name="struts.multipart.maxStringLength" value="45000000" />



    <package name="spareTest" extends="default">

        <default-interceptor-ref name="paramsPrepareParamsStack"/>



        <action name="testUpload" class="indexAction" method="uploadTest">

            <result name="input" >/WEB-INF/pages/main/formUpload.jsp</result>

            <result name="success" type="redirectAction">home</result>

        </action>



        <action name="testFile" class="indexAction" method="edit">

            <result name="input" >/WEB-INF/pages/main/formUpload.jsp</result>

        </action>



    </package>


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to