zoukankan      html  css  js  c++  java
  • 第三章 struts2 (二)

    1:Stream

     <result-type name="stream" class="org.apache.struts2.result.StreamResult"/>

    A custom Result type for sending raw data (via an InputStream) directly to the HttpServletResponse. Very useful for allowing users to download content.

    This result type takes the following parameters: 

    • contentType - the stream mime-type as sent to the web browser (default = text/plain).
    • contentLength - the stream length in bytes (the browser displays a progress bar).
    • contentDisposition - the content disposition header value for specifing the file name (default = inline, values are typically attachment;filename="document.pdf".
    • inputName - the name of the InputStream property from the chained action (default = inputStream).

    1:开发一个action,提供一个getInputStream():InputStream

    public class DownAction extends ActionSupport {

    @Override

    public String execute() throws Exception {

    System.err.println("判断用户的积分,判断用户的状态..");

    return SUCCESS;

    }

    public InputStream getInputStream() throws Exception {

    String path = ServletActionContext.getServletContext().getRealPath("/files/yy.zip");

    InputStream in = new FileInputStream(path);

    return in;

    }

    }

    2:配置到stuts.xml中且返回的<result type=”stream”/>

    <action name="down" class="cn.down.DownAction">

    <result type="stream"></result>

    </action>

    <action name="down" class="cn.down.DownAction">

    <result type="stream">

    <param name="contentType">application/force-download</param>

    <param name="contentDisposition">attachment;filename="document.zip"</param>

    </result>

    </action>

    附加:

     1:下载时修改名称

    String name = "我的文件.zip";

    name = URLEncoder.encode(name,"UTF-8");

    ActionContext.getContext().put("fileName", name);

    XML中取值:

       在XML中使用OGNL取值,是通过${..}

    <action name="down" class="cn.struts2.down.DownAction">

    <result type="stream">

    <param name="contentType">application/force-download</param>

    <param name="contentDisposition">attachment;filename="${fileName}"</param>

    </result>

    </action>

    --通过inputName指定方法名:

    <action name="down" class="cn.struts2.down.DownAction">

    <result type="stream">

    <param name="contentType">application/force-download</param>

    <param name="contentDisposition">attachment;filename="${fileName}"</param>

    <!-- DownAction.getDownfile():InputStream -->

    <param name="inputName">downfile</param>

    </result>

    </action>

    public InputStream getDownfile() throws Exception {

    String path = ServletActionContext.getServletContext().getRealPath("/files/yy.zip");

    InputStream in = new FileInputStream(path);

    return in;

    }

      

      

     

      

  • 相关阅读:
    云路五年 未来正来
    免费公测:RDS只读实例
    【Open Search产品评测】-- 淘点点:基于OpenSearch,轻松实现一整套O2O类搜索解决方案
    阿里云启动“云合计划” 培育中国“微软”级企业
    【Open Search产品评测】- 来往,7天轻松定制属于自己的搜索引擎
    水塘抽样算法
    如何调度考生的座位
    如何去除有序数组的重复元素
    二分查找高效判定子序列
    如何k个一组反转链表
  • 原文地址:https://www.cnblogs.com/carsar/p/5597159.html
Copyright © 2011-2022 走看看