This tutorial will help you understanding how to create a Struts 2 action class that allows users to download files from server to their local computer.
Struts 2 provides a custom result type called “stream” that performs file download by “streaming” an InputStream of a file to the client through the HttpServletResponse. Here’s an example that shows how to define this result type element in struts.xml:
The struts.xml file should have the following mapping
Note that we declare a property placeholder called “fileName” which will be dynamically updated by Struts. Now in the action class, we need to declare a variable with the same name along with getter and setter method. By this way we have configured the download file name dynamically.
In addition to that, we must define an InputStream object which has the same name as the name specified by the inputName parameter in struts.xml. All the variables must have appropriate getter methods.