struts2文件下载 <result type="stream">

<!--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";
         }
     }
 }
时间: 2024-07-28 20:22:09

struts2文件下载 <result type="stream">的相关文章

Struts2文件下载&lt;*&gt;servlet-api的方式文件下载

Struts2文件下载 1 public class DownLoadAction extends ActionSupport{ 2 3 private static final long serialVersionUID = 1L; 4 //要下载的文件名 5 private String filename; 6 public String getFilename() { 7 return filename; 8 } 9 public void setFilename(String filen

解决struts2文件下载中文名问题

package com.bgsnewlook.action; import com.opensymphony.xwork2.ActionSupport;import org.apache.struts2.ServletActionContext; import java.io.InputStream; import java.net.URLEncoder; public class Download extends ActionSupport {    private String inputP

struts2文件下载及 &lt;param name="inputName"&gt;inputStream&lt;/param&gt;的理解

struts.xml文件配置: [html] view plaincopy <span style="font-size:16px;"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" &q

Struts2之struts2文件下载详解

一.学习案例:通过在downloadfile.jsp页面点击"下载文件",查看文件是否可以成功下载. 二.案例分析:struts2文件下载只需简单配置即可. a)先在下载页面(downloadfile.jsp)创建下载文件的链接 <a href="downloadFile">下载文件</a> b)创建action(DownloadFileAction.java) 只需创建一个InputStream类型类型的get方法. 注意:此处方法名规则为

struts2文件下载,动态设置资源地址

转自:http://blog.csdn.net/ctrl_shift_del/article/details/6277340 1 ServletActionContext.getServletContext().getResourceAsStream("/"+tempfile); 这是java加载资源的方法,所谓资源,实际上是任何一个文件,但特别的 是,getResourceAsStream这个方法不使用绝对路径,而是使用相对于classpath环境变量的相对路径.所 以,如果写: 1

struts2——文件下载自定义文件名,包括中文

1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <%@taglib uri="/struts-tags" prefix="s"%> 4 <!DOCTYPE html> 5 <html> 6 <head> 7 &l

struts2文件下载相关信息

struts.xml文件配置: [html] view plaincopy <span style="font-size:16px;"><?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" &q

struts2 文件下载 报错

程序错误: 学习struts框架时,关于文件下载部分,利用struts中的stream结果类型来实现,配置完成之后,运行程序,报错如下: HTTP Status 500 - Can not find a java.io.InputStream with the name [downFile] in the invocation stack. Check the <param name="inputName"> tag specified for this action. 错

struts2文件下载出现Can not find a java.io.InputStream with the name的错误

今天在用struts2就行文件下载时出现如下错误: Servlet.service() for servlet default threw exception java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [imageStream] in the invocation stack. Check the <param name="inputName">