Language Translator

Downloading File in struts using DownloadAction, Struts Download File Action, Download Code for file download in struts.

 Implementing a DownloadAction
You need to extend org.apache.struts.actions.DownloadAction and implement the getStreamInfo() method. Optionally you can also override the getBufferSize() method if you require a different buffer size from the default.
Implement the getStreamInfo() Method
The getStreamInfo() method returns a StreamInfo object - which is an inner interface of the DownloadAction. The DownloadAction provides two concrete implementations (static inner classes) of the StreamInfo interface:
* FileStreamInfo - Simplifies downloading of a file from disk - need to pass a java.io.File object to the constructor along with the content type.
* ResourceStreamInfo - simplifies downloading of a web application resource - need to pass the ServletContext, path and content type to its constructor.
In the examples below referring by link, I have also provided a Byte array implementation of the StreamInfo interface. Implement the getBufferSize() Method.
The DownloadAction, by default, returns a buffer size of 4096. Optionally, this may be overriden to customize the size of the buffer used to transfer the file.
Link Shows here  below is having three examples:
* using a File
* using a web application resource
* using a byte array.
File name: DownloadFileInStruts.zip
File size:5.73 KB


No comments:

Post a Comment