Struts2 file download requires action to provide a way to return the InputStream stream, which represents the entry for the downloaded file.
One: Write Action class
Downloadaction.java
PackageAction;ImportCom.opensymphony.xwork2.ActionSupport;ImportJava.io.FileInputStream;ImportJava.io.InputStream;/*** Created by Cxspace on 16-7-12.*/ Public classDownloadactionextendsactionsupport{//This property value can be specified dynamically in the configuration file, and the path to the file to be downloaded PrivateString InputPath;
PublicString Getinputpath () {returnInputPath; } //method of Dependency Injection changing attribute value Public voidSetinputpath (String inputpath) { This. InputPath =InputPath; }
/x
Define a method to return InputStream, which will act as the entry for the downloaded file, and need to configure the InputName parameter specified by the stream type result
InputName parameter value is the method to remove the get prefix, the first letter lowercase string
x/ PublicInputStream Gettargetfile ()throwsException {return NewFileInputStream (InputPath); }}
Two: Configure action
Configuring the desired key download, the key is to configure a result of type stream
Four key attributes to be specified in the result
ContentType: Specifies the file type of the downloaded file
InputName: Specifies the ingress input stream for the downloaded file
Contentdispostion: Specifies the file name of the download
BufferSize: Specifies the buffer size when downloading a file
PackageAction;
ImportCom.opensymphony.xwork2.ActionSupport;
ImportJava.io.FileInputStream;
ImportJava.io.InputStream;
/**
* Created by Cxspace on 16-7-12.
*/
Public classDownloadactionextendsactionsupport{
//This property value can be specified dynamically in the configuration file
PrivateStringInputPath;
PublicStringGetinputpath() {
returnInputPath;
}
//method of Dependency Injection changing attribute value
Public voidSetinputpath(String InputPath) {
This.InputPath= InputPath;
}
PublicInputStreamGettargetfile()throwsException {
return newFileInputStream (InputPath);
}
}
struts-Basic content -8-File download