Of course you can.
You must output the Stream Result type from your Action, and specify a parametric contentType, for example:
Struts.xml
<result name="success" type="stream">
<param name="contentType">${yourContentType}</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="${yourFileName}"</param>
<param name="bufferSize">1024</param>
</result>
Action
@Getter @Setter private InputStream inputStream;
@Getter private String yourContentType;
@Getter private String yourFileName;
public String execute() throws Exception {
yourContentType = "audio/mpeg";
yourFileName = "yourStuff.mp3";
byte[] yourContent = loadTheContentInSomeWay();
setInputStream(new ByteArrayInputStream(yourContent));
return SUCCESS;
}
You can parameterize the contentDisposition
part to specify when a file must be opened as attachment
(ask for download) or inline
(open in browser) according to your needs.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…