struts2文件下载 <result type="stream">
2013-01-23 17:58
609 查看
<!--struts.xml配置-->
<action name="download" class="com.unmi.action.DownloadAction">
<result name="success" type="stream"><!--type 为 stream 应用 StreamResult 处理-->
<param name="contentType">application/octet-stream</param><!--默认为 text/plain-->
<!-- 默认就是 inputStream,它将会指示 StreamResult 通过 inputName 属性值的 getter 方法,
比如这里就是 getInputStream() 来获取下载文件的内容,意味着你的 Action 要有这个方法 -->
<param name="inputName">inputStream</param>
<!-- 默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文
件保有存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名,
这里使用的是动态文件名,${fileName}, 它将通过 Action 的 getFileName() 获得文件名 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param><!-- 输出时缓冲区的大小 -->
</result>
</action>
Java代码:
public class NetbookSerialAction {
public String execute() throws Exception {
//这里可加入权限控制
return "success";
}
//获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容
public InputStream getInputStream() throws Exception {
//return new FileInputStream("somefile.rar"); 直接下载 somefile.rar
//和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1
return new ByteArrayInputStream("Struts2 文件下载测试".getBytes());
}
//对于配置中的 ${fileName}, 获得下载保存时的文件名
public String getFileName() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String fileName = "序列号(" + df.format(new Date()) + ").txt";
try {
//中文文件名也是需要转码为 ISO8859-1,否则乱码
return new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
return "impossible.txt";
}
}
}
<action name="download" class="com.unmi.action.DownloadAction">
<result name="success" type="stream"><!--type 为 stream 应用 StreamResult 处理-->
<param name="contentType">application/octet-stream</param><!--默认为 text/plain-->
<!-- 默认就是 inputStream,它将会指示 StreamResult 通过 inputName 属性值的 getter 方法,
比如这里就是 getInputStream() 来获取下载文件的内容,意味着你的 Action 要有这个方法 -->
<param name="inputName">inputStream</param>
<!-- 默认为 inline(在线打开),设置为 attachment 将会告诉浏览器下载该文件,filename 指定下载文
件保有存时的文件名,若未指定将会是以浏览的页面名作为文件名,如以 download.action 作为文件名,
这里使用的是动态文件名,${fileName}, 它将通过 Action 的 getFileName() 获得文件名 -->
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param><!-- 输出时缓冲区的大小 -->
</result>
</action>
Java代码:
public class NetbookSerialAction {
public String execute() throws Exception {
//这里可加入权限控制
return "success";
}
//获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容
public InputStream getInputStream() throws Exception {
//return new FileInputStream("somefile.rar"); 直接下载 somefile.rar
//和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1
return new ByteArrayInputStream("Struts2 文件下载测试".getBytes());
}
//对于配置中的 ${fileName}, 获得下载保存时的文件名
public String getFileName() {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
String fileName = "序列号(" + df.format(new Date()) + ").txt";
try {
//中文文件名也是需要转码为 ISO8859-1,否则乱码
return new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
return "impossible.txt";
}
}
}
相关文章推荐
- struts2文件下载 <result type="stream">
- struts2文件下载 <result type="stream">
- struts2文件下载 <result type="stream">
- 上struts2的xml在<result type="redirect">参数问题
- struts2 <result type = "stream">
- <input type="file" />浏览时只显示指定文件类型
- struts2文件下载及 <param name="inputName">inputStream</param>的理解
- <input type="file" />浏览时只显示指定文件类型
- <input type="file" />浏览时只显示指定文件类型
- struts2文件下载及 <param name="inputName">inputStream</param>的理解
- 使用<input type="file">实现文件上传
- <input type="file" />浏览时只显示指定文件类型
- Struts2通配符映射/Struts <action name="/*/*" method="{2}" class="com.jxc.action.{1}Action"> <result
- 关于struts2的xml中<result type="redirect">参数问题
- struts.xml配置文件中result的语法:<result name="" type="">xxxxx</result>
- 用struts2开发,<result type="json"></result>是什么意思?
- <input type="file">定义样式并获取上传文件路径及指定文件类型
- struts2文件下载及 <param name="inputName">inputStream</param>的理解
- struts.xml配置文件中result的语法:<result name="" type="">xxxxx</result>
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data