这正是StreamResult结果类型的用途。
例子:
在您的 WebWork XML 中:
<result name="download" type="stream">
<param name="contentDisposition">filename=report.txt</param>
<param name="contentType">text/plain;charset=UTF-8</param>
<param name="inputName">inputStream</param>
<param name="bufferSize">1024</param>
</result>
在你的行动中:
public InputStream getInputStream() {
try {
return new ByteArrayInputStream(getOtchet().getBytes("UTF-8"));
}
catch (UnsupportedEncodingException ex) {
// Shouldn't happen with UTF-8.
ex.printStackTrace();
}
}
public String doDownload() {
if (SUCCESS.equals(execute()) {
return "download";
}
else {
return ERROR;
}
}