Struts2 Fileupload giving null file in the action class
Asked Answered
T

1

1

I am trying to implement the file upload process in my web application using struts2 fileUpload interceptor. below is my code in

index.jsp

<tags:form action="fileUpload" method="post" enctype="multipart/form-data">
   <tags:file name="fileUpload" label="Choose File"/>
   <tags:submit value="Upload"/>     
</tags:form> 

struts.xml

<action name="fileUpload" class="com.hibernate.action.FileUploadAction">
    <interceptor-ref name="fileUploadStack"/>
    <interceptor-ref name="fileUpload">
        <param name="maximumSize">1024000</param>
        <param name="allowedTypes">application/pdf</param>
    </interceptor-ref>
    <result name="success">/viewChapters.jsp</result>
</action>

FileUploadAction.java

public class FileUploadAction extends ActionSupport
{
private File fileUpload;
private String contentType;
private String fileName;
private String destPath;
/// setter and getter methods
 public String execute()
{
    destPath="C:\\WebPortal_testing";
    try
    {
        System.out.println("Source File Name:"+fileUpload);
        System.out.println("Destination File Name:"+fileName);

        File destFile= new File(destPath,fileName);
        FileUtils.copyFile(fileUpload, destFile);
    }
    catch(IOException exception)
    {
        exception.printStackTrace();
        return ERROR;
    }
    return SUCCESS;
 }

when I select a pdf file in the index.jsp page and click on upload button it is giving null value to the fileUpload field of the action class.

I am executing the application in debug mode and gave this

System.out.println("Source File Name:"+fileUpload);

to check what it is returning and I am getting null.

Tanguay answered 13/11, 2015 at 16:2 Comment(0)
M
2

1. Interceptor configuration is wrong

FileUploadStack is:

<!-- Sample file upload stack -->
<interceptor-stack name="fileUploadStack">
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="basicStack"/>
</interceptor-stack>

then what you're really defining is:

    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="basicStack"/>
    <interceptor-ref name="fileUpload">
        <param name="maximumSize">1024000</param>
        <param name="allowedTypes">application/pdf</param>
    </interceptor-ref>

Using

  • two times the fileUpload interceptor
  • applying your limitations on maximumSize and allowedTypes only to the second.

Just do

<interceptor-ref name="fileUploadStack">
    <param name="fileUpload.maximumSize">1024000</param>
    <param name="fileUpload.allowedTypes">application/pdf</param>
</interceptor-ref>

2. File attributes are wrong

Content type and file name attributes must start with the File attribute name.

In your case:

private File fileUpload;
private String fileUploadContentType;
private String fileUploadFileName;

You can find a full example on this question.


3. You are printing the File instead of the filename

System.out.println("Source File Name:"+fileUpload);

That is the file, not the filename, and btw the filename is passed in the other variable.


Fix this and retry. Also note that is not safe to use <tags: as prefix when the whole world is using <s:. There's no gain in doing that, only complications. Just use <s:.

Mahdi answered 13/11, 2015 at 16:38 Comment(6)
Thanks for your response. regarding your 3. You are printing the File instead of the filename I am using this only for testing purpose. I have updated every thing as per your suggestions but no luck. I am still getting the same issue.Tanguay
Then check carefully the setters, or post more details because everything now seems fineMahdi
sorry for the delay. I am still facing the issue. I have put that part of implementation on hold and working on other parts of the application where I am facing few other issues. I will post those doubts in a while please comment on them when free. my sincere thanks for your helpTanguay
Ok, can you edit your question by applying the latest code tried (the one with the modifies suggested in the answer) ? So I can check what is going southMahdi
@AndreaLigios The struts2 fileupload interceptor is not checking content type whether the file content is image or not. While uploading text file as image (I changed extension of file from txt to png) its accepting means file upload has been done. I think its just checking extension. I also set allowedTypes as image/bmp,image/png,image/gif,image/jpeg,image/jpg. Correct me if I did wrong. I'm using struts2.3.32 version. If I upload actual image its uploading, so its correct.Inquiring
@Satya it's a browser fault. The browser detects the contentType to pass mainly basing on the extension; hence it will often send a wrong contentType if the extension is not correct. You can check it by examining the contentType in the request sent. Read more: https://mcmap.net/q/1329093/-can-39-t-struts2-detect-the-content-of-a-file-renaming-extension-troubleMahdi

© 2022 - 2024 — McMap. All rights reserved.