<action name="export" class="co.dnl.editor.action.ExportAction">
<result name="success" type="stream">
<!-- param name="contentType">application/zip</param-->
<param name="contentType">application/zip</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">
attachment;filename="mycontent.zip"//根据需要添加红色部分
</param>
<param name="bufferSize">1024</param>
</result>
</action>
写一个action, 确保有inputStream变量:
public class ExportAction extends BaseAction
{
private InputStream inputStream;
public String execute()
{
log.info("in ExportAction");
String str = "hello, world. I am from struts2 output";
inputStream = new ByteArrayInputStream(str.getBytes());
return Action.SUCCESS;
}
public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
}
访问: http://localhost:8080/yourApp/export.action
导出下载了。
over
http://topic.youkuaiyun.com/u/20091201/22/8f9bdfd3-5a1e-479e-ac51-291982d3e74b.html
在进行Web开发时,可能遇到遇到以下几种需求:
l 希望某类或者某已知MIME 类型的文件(比如:*.gif;*.txt;*.htm)能够在访问时弹出“文件下载”对话框。
l 希望客户端下载时以指定文件名显示。
l 希望某文件直接在浏览器上显示而不是弹出文件下载对话框。
对于上面的需求,使用Content-Disposition属性就可以解决。下面是代码示例:
response.setHeader("Content-disposition", "attachment;filename=" + fileName)。
//Content-disposition为属性名。
//attachment表示以附件方式下载。如果要在页面中打开,则改为inline。
//filename如果为中文,则会出现乱码。解决办法有两种:
//1、使用fileName = new String(fileName.getBytes(), "ISO8859-1")语句
//2、使用fileName = HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8)语句
本文详细介绍了如何在Struts2框架下配置一个用于文件导出的action,包括参数设置、输入流变量使用及导出文件的下载过程,并提供了实现代码示例。
2983

被折叠的 条评论
为什么被折叠?



