Search code examples
javahtmljspfile-uploadstruts2

How to get filename selected from file dialog in JSP and Struts action class?


I am developing a web application in which for User Registration the user has to select a profile picture and then click on upload button.

So, my question is how to get FileName which user has selected and display.

I am using Struts 2 and JSP.


Solution

  • If you use the property uploadFileName along with upload property to the action that accepts uploading a file, i.e. the fileUpload interceptor is applied to the action configuration then it will be populated. There's also uploadContentType attribute that could be populated.

    private File upload;
    private String uploadContentType;
    private String uploadFileName;
    //getters ans setters assumed
    

    in the JSP use the name attribute to map it to the action

    <s:file name="upload"/>