Struts2 Fileupload giving null file in the action class

[亡魂溺海] 提交于 2019-12-01 12:54:58
Andrea Ligios

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:.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!