=============================struts File Download ==================================
Step One: JSP page
<a href= "Download.action? fileName=img_0443.jpg "> Click here to download images </a>
Step Two: Action page
Package org.zm.action;
Import Java.io.BufferedInputStream;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.InputStream;
Import Org.apache.struts2.ServletActionContext;
Import Com.opensymphony.xwork2.ActionSupport;
public class Downloadaction extends actionsupport{
Read the directory of downloaded files
Private String InputPath;
filename of the download file
Private String FileName;
Read the input stream of the downloaded file
Private InputStream InputStream;
Types of download files
Private String Conetnttype;
Create InputStream input stream
Public InputStream getInputStream () throws filenotfoundexception{
String Path=servletactioncontext.getservletcontext ().
Getrealpath (InputPath);
return new Bufferedinputstream (new FileInputStream (path+ "\ \" +
FileName));
}
@Override
Public String execute () {
return SUCCESS;
}
public void Setinputstream (InputStream inputstream) {
This.inputstream = InputStream;
}
Public String GetFileName () {
return fileName;
}
public void Setfilename (String fileName) {
This.filename = FileName;
}
Public String Getconetnttype () {
return conetnttype;
}
Public String Getinputpath () {
return inputpath;
}
public void Setinputpath (String inputpath) {
This.inputpath = InputPath;
}
public void Setconetnttype (String conetnttype) {
This.conetnttype = Conetnttype;
}
}
Step Three: Struts.xml file
<action name= "Download" class= "Org.zm.action.DownLoadAction" >
<param name= "InputPath" >/upload</param>
<result name= "Success" type= "Stream" >
<param name= "ContentType" >image/pjpeg</param>
<param name= "InputName" >inputStream</param>
<param name= "contentdisposition" >attachment;filename= "${filename}" </param>
<param name= "BufferSize" >4096</param>
</result>
</action>
Hint: Stream result type
ContentType: Sets the MIME type to send to the browser
ContentLength: File Size
Contentdisposition: Sets the value of the Content-disposition parameter for the HTTP header information of the response
InputName: Specifies the property name of the InputStream type provided by the action
BufferSize: Set the size of the buffer when reading and downloading files
The variable names in both locations of the Struts.xml file and action must be correct.
Struts File Download