Struts 2 的stream result用法
来源:互联网 发布:java取余数和取整数 编辑:程序博客网 时间:2024/05/08 12:06
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>
class代码
import <a href="http://lib.csdn.net/base/17" class='replace_word' title="Java EE知识库" target='_blank' style='color:#df3434; font-weight:bold;'>Java</a>.io.*; import java.text.*; import java.util.Date; /** * 文件下载的 Action * @author Unmi */ public class NetbookSerialAction { public String execute() throws Exception { //这里可加入权限控制 return "success"; } //获得下载文件的内容,可以直接读入一个物理文件或从<a href="http://lib.csdn.net/base/14" class='replace_word' title="MySQL知识库" target='_blank' style='color:#df3434; font-weight:bold;'>数据库</a>中获取内容 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"; } } }
0 0
- Struts 2 的stream result用法
- Struts 2 的stream result用法
- Struts 2 的stream result用法
- Struts2 result type="stream"的用法
- struts 2 的Result
- struts 2 的Result
- struts2里的struts.xml中 当result的type="stream"
- struts 2 result type
- 使用stream类型的Result实现Ajax
- 使用Stream类型的Result实现Ajax
- Struts中配置的result
- struts.xml的result类型
- struts 2 result-types 备忘
- Java8---Stream的基本用法(2)
- adodb.stream的用法
- Stream的用法
- struts配置的result属性。 result的动态配置 . result 全局结果 global-result
- struts result
- 如何给GridView添加分割线
- 市长信箱邮件查询服务: 使用SpringBoot搭建基础
- 计算机程序的构造和解释 练习 1.35 利用不动点求黄金分割
- sky_Valid Anagram
- 选择相册照片并且显示(拿到返回的图片地址)
- Struts 2 的stream result用法
- JavaScript类和模块
- Stream和Buffer
- HDU 5696 区间的价值 (DFS)
- java程序使用JDBC接口连接数据库
- HTTP长连接与短连接
- 练习三1014
- 使用javabean实现用户登录
- AutoCompleteTextView和GridView