1:stream
<result-type name="stream" class="Org.apache.struts2.result.StreamResult"/>
A custom Result type for sending raw data (via an InputStream) directly to the HttpServletResponse. Very useful for allowing users to download content.
This result type takes the following parameters:
- ContentType -The stream Mime-type as sent to the Web browser (default = Text/plain).
- contentlength -The stream length in bytes (The browser displays a progress bar).
- contentdisposition -The content disposition header value for specifing the file name (default = inline, values a Re typically attachment;filename= "Document.pdf".
- InputName -the name of the InputStream property from the chained action (default = InputStream).
Step 1: Develop an actionthat provides a getinputstream (): InputStream
Public class downaction extends Actionsupport {
@Override
Public String Execute () throws Exception {
System. err. println (" Judging the user's points, judging the state of the user . ");
return SUCCESS;
}
Public InputStream getInputStream () throws Exception {
String Path = Servletactioncontext. Getservletcontext (). Getrealpath ("/files/yy.zip");
InputStream in = new fileinputstream (path);
return in;
}
}
Step 2: Configure to stuts.xml and return the <result type="stream"/>
<action name= "Down" class="Cn.down.DownAction">
<result type="stream"></result>
</action>
<action name= "Down" class="Cn.down.DownAction">
<result type="Stream">
<param name="ContentType">application/force-download</param>
<param name="contentdisposition">attachment;filename= "Document.zip" </param>
</result>
</action>
Additional:
1: Modify name when downloading
String name = " my file . zip";
Name = Urlencoder. encode (Name, "UTF-8");
Actioncontext. GetContext (). Put ("FileName", name);
To take a value in XML :
use OGNL in XML to take a value, is through ${...}
<action name= "Down" class="Cn.struts2.down.DownAction">
<result type="Stream">
<param name="ContentType">application/force-download</param>
<param name="contentdisposition">attachment;filename= "${filename}" </param>
</result>
</action>
-- Specify the method name by inputname :
<action name= "Down" class="Cn.struts2.down.DownAction">
<result type="Stream">
<param name="ContentType">application/force-download</param>
<param name="contentdisposition">attachment;filename= "${filename}" </param>
<!-- in downaction.getdownfile (): InputStream--
<param name="InputName">downfile</param>
</result>
</action>
Public InputStream Getdownfile () throws Exception {
String Path = Servletactioncontext. Getservletcontext (). Getrealpath ("/files/yy.zip");
InputStream in = new fileinputstream (path);
return in;
}
Struts2 (b)