浅谈Struts2下载文件的方法实现

开发 后端
本文将介绍Struts2下载文件的方法实现,包括对缓冲区的处理,下载文件的来源流处理等等。希望对大家操作Struts2下载文件有帮助。

Struts2下载文件实现的说明

contentType

内容类型,和互联网MIME标准中的规定类型一致,例如text/plain代表纯文本,text/xml表示XML,image/gif代表GIF图片,image/jpeg代表JPG图片

inputName

下载文件的来源流,对应着action类中某个类型为Inputstream的属性名,例如取值为inputStream的属性需要编写getInputStream()方法

contentDisposition

文件下载的处理方式,包括内联(inline)和附件(attachment)两种方式,而附件方式会弹出文件保存对话框,否则浏览器会尝试直接显示文件。取值为:

attachment;filename="struts2.txt",表示文件下载的时候保存的名字应为struts2.txt。如果直接写filename="struts2.txt",那么默认情况是代表inline,浏览器会尝试自动打开它,等价于这样的写法:inline; filename="struts2.txt"

bufferSize

下载缓冲区的大小

Struts2下载实现源代码

< ?xml version="1.0" encoding="UTF-8" ?>
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "< A href="http://struts.apache.org/dtds/struts-2.0.dtd">
http://struts.apache.org/dtds/struts-2.0.dtd< /A>">
    < PACKAGE name="default" extends="struts-default">
        < !-- 在这里添加Action定义 -->

        < !-- 简单文件下载 -->
        < ACTION class=example.FileDownloadAction name="download">
            < RESULT name="success" type="stream">
                < PARAM name="contentType">text/plain< /PARAM>
                < PARAM name="inputName">inputStream< /PARAM>
                < PARAM name="contentDisposition">attachment;filename="struts2中文.txt"< /PARAM>
                < PARAM name="bufferSize">4096< /PARAM>
            < /RESULT>
        < /ACTION>
        < BR>        < !-- 文件下载,支持中文附件名 -->
        < ACTION class=example.FileDownloadAction2 name="download2">
            < !-- 初始文件名 -->
            < PARAM name="fileName">Struts中文附件.txt< /PARAM>
            < RESULT name="success" type="stream">
                < PARAM name="contentType">text/plain< /PARAM>
                < PARAM name="inputName">inputStream< /PARAM>
                < !-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
对应action类中的方法 getDownloadFileName() -->
                < PARAM name="contentDisposition">attachment;filename="${downloadFileName}"< /PARAM>
                < PARAM name="bufferSize">4096< /PARAM>
            < /RESULT>
        < /ACTION>
       
        < !-- 下载现有文件 -->
        < ACTION class=example.FileDownloadAction3 name="download3">
            < PARAM name="inputPath">/download/系统说明.doc< /PARAM>
            < !-- 初始文件名 -->
            < PARAM name="fileName">系统说明.doc< /PARAM>
            < RESULT name="success" type="stream">
                < PARAM name="contentType">application/octet-stream;charset=ISO8859-1< /PARAM>
                < PARAM name="inputName">inputStream< /PARAM>
                < !-- 使用经过转码的文件名作为下载文件名,downloadFileName属性
对应action类中的方法 getDownloadFileName() -->
                < PARAM name="contentDisposition">attachment;filename="${downloadFileName}"< /PARAM>
                < PARAM name="bufferSize">4096< /PARAM>
            < /RESULT>
        < /ACTION>        
    < /PACKAGE>

【编辑推荐】

  1. 在Eclipse中开发struts应用程序
  2. 手把手教你在Eclipse中配置开发Struts
  3. Eclipse下开发struts完整解决乱码问题
  4. Struts相关背景介绍
  5. 使用Easy Struts for Eclipse开发Struts
责任编辑:张燕妮 来源: javaeye.com
相关推荐

2009-06-08 16:44:00

Struts2文件上传

2009-06-04 09:41:50

struts2上传文件

2009-06-04 08:01:25

Struts2拦截器原理

2009-06-08 16:44:00

2009-06-05 09:58:20

struts2验证用户注册

2009-06-04 08:34:24

Struts2配置struts.xml

2011-04-28 09:52:04

Struts2

2013-05-28 11:29:19

struts2

2009-07-29 09:54:34

struts2和str

2011-06-28 09:14:23

Struts 2WebWork

2009-06-25 15:50:03

Struts2教程上传任意多个文件

2012-04-25 10:14:40

JavaStruts

2020-10-25 16:53:56

Linux 开源操作系统

2009-06-18 11:48:07

Struts2POI生成文件

2009-07-06 16:18:51

Servlet下载文件

2009-06-25 15:59:21

Struts2教程拦截器

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-06-25 15:33:12

Struts2教程使用validate验证数据

2009-02-04 10:51:07

2009-06-05 10:52:45

struts2深入详解配置文件
点赞
收藏

51CTO技术栈公众号