Struts2文件下载

作者在 2018-05-20 12:00:50 发布以下内容

stream结果类型

        >>将文件数据(通过InputStream获取)直接写入响应流

        >>相关参数的配置

      ————————————————————————————

        contentType        设置发送到浏览器的MIME类型

        contentLength      设置文件的大小

        contentDisposition    设置响应的HTTP头信息中的Content-Disposition参数值

        inputName            指定Action中提供的inputStream类型的属性名称

        bufferSize            设置读取和下载文件时的缓冲区大小

    ———————————————————————————————

        指定文件下载的类型

    Word            application/maword

    Excel            application/vnd.ms-excel

    PPT              application/vnd.ms-powerpoint

    图片             image/gif,    image/bmp,    image/jpeg

    文本文件      text/plain

    html网页      text/html

    任意二进制数据    application/octet-stream

    ____________________________________________________

      实现步骤

                >>编写下载文件Action

                        >>获取InputStream输入流

                >>配置Action

                        >>指定下载文件的类型、下载形式等

    ——————————————————————————————————

public class FileDownAction extends ActionSupport{

        private String inputPath;        //读取下载文件的目录

        private String fileName;          //下载文件的文件名

        private InputStream inputStream; //读取下载文件的输入流

        //创建InputStream输入流

        public InputStream getInputStream() throws FileNotFoundException{

                String path= ServletActionContext.getServletContext().getRealPath(inputPath);

                return new BufferedInputStream(new FileInputStream(path+"\\"+fileName));

        }

}

    ______________________________________________________________

    配置Action

    <result name="success" type="stream">

            <param name="contentType">application/octet-stream</param>

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

            <param name="contentDisposition">

                    attachment;filename="${fileName}"

            </param>

            <param name="bufferSize">5000</param>

    </result>

Struts2 | 阅读 992 次
文章评论,共0条
游客请输入验证码
文章分类
最新评论