Download Action in struts 1.3 with example . How to download file from server using struts?

Do you need to write a code to download a file from server to client (local machine) using your application?  Are you using struts frame work in your project ? You are lucky to use struts because Struts provides a built-in action called DownloadAction that is a abstract class. Instead of creating the action by extending simple Action class and writing all necessary code for downloading files, just extend org.apache.struts.actions.DownloadAction and implement the getStreamInfo() method. It minimizes the amount of special coding required to download a file / web application resource because DownloadAction provides all the infrastructure code including the code to read and copy bytes from an InputStream to an OutputStream

Steps to download a file / web application resource using Struts DownloadAction

1. Create DownloadAction sub class (in our example FileDownload / ResourceDownload) by extending org.apache.struts.actions.DownloadAction abstract class

2. Implement the abstract method getStreamInfo() of DownloadAction class , which returns StreamInfo instance of FileStreamInfo / ResourceStreamInfo class.  i.e. DownloadAction provides two static inner classes (FileStreamInfo, ResourceStreamInfo) which implements StreamInfo interface

StreamInfo Interface – returns details about the file to be downloaded by the DownloadAction using the following two methods

getContentType() – to return the content type of the stream to be downloaded

getInputStream()– returns an input stream on the content to be downloaded. This two methods are implemented by the following classes.

FileStreamInfo class– constructor accepts java.io.File object and the content type and returns an input stream on the file to be downloaded. The stream will be closed by the DownloadAction.

ResourceStreamInfo class -constructor accepts the ServletContext, path and content type and returns an input stream on the resource to be downloaded. The input stream will be closed by the DownloadAction.

3. Override getBufferSize() method if you want to customize the size of the buffer used to transfer the file. DEFAULT_BUFFER_SIZE is 4MB

4. Inside getStreamInfo() Method , set the Content Disposition to either download the file or open up in the browser.

To download, use “attachment; filename=test.pdf” , to open up in the browser, use “inline; filename=test.jpg”

Statement to set Content Disposition : response.setHeader(“Content-disposition”, “attachment; filename=” + fileName);

If Content Disposition is not set, the file is opened in the browser, if the content is supported .

5. Set the contentType = “application/pdf” for dowloading PDF file, contentType = “application/vnd.ms-excel” for downlading excel file, contentType = “image/jpeg” for downloading images and so on…..

More Methods on DownloadAction abstract class:

copy – reading bytes from an InputStream and writing to an OutputStream.

execute Creates the HTTP response by processing the HTTP request. Calls copy method to copy bytes from an InputStream to an OutputStream.

Note : DownloadAction is available from struts 1.2.6 onwards. If you use older struts version , then download struts library files for the version 1.2.6 , put the files in lib folder .

Sample code:

1. To download file (For eg. pdf )

2.  Web application resource (For eg. image file)

To download File 

To download Web Application Resource

The above codes gets the path from the parameter attribute in the struts-config.xml action mapping. For file dowload , the path is d:\\serverDir in the server . For Resource download , the path is /images in application path of the server.

in struts-config file

JSP code for File Download

JSP code for Web Application Resource Download

You may also like

Leave a Reply