• 第三章 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;

    }

      

      

     

      

  • 相关阅读:
    CentOS 7.3报Centos7_Base库缺少GPG公钥
    nginx重写(隐藏)index.php目录
    工作经历简写
    Centos7.4安装htop
    nginx 超时时间配置说明
    c#中数据从数据库到客户端主要几种的导出方式(导出到excel,导出到word)
    C#操作word文档如何设置表格的行高
    Windows计划任务定时启动程序运行时发生错误的解决办法
    Asp.Net MVC中请求json文件时返回404 not found的解决办法
    Angularjs 如何自定义Img的ng-load 事件
  • 原文地址:https://www.cnblogs.com/carsar/p/5597159.html
Copyright © 2020-2023  润新知