struts2 文件流, 文件输出到浏览器 文件下载

众所周知,直接在页面中插入a标签引入文件地址可以解决,但是这样即暴露了项目路径而且再某些情况下会有一些小问题,最好的办法是用I/O流,

(readlinne方式),今天我记录的是struts2中的文件输入输出框架的用法:

 

struts.xml配置文件片段:

 

<result name="download" type="stream">
                   <!-- 下载文件类型 -->
                   <param name="contentType">text/plain</param>
                   <!-- 下载对话框所弹出的文件名 -->
                   <param name="contentDisposition">
                    attachment;fileName=${currentLogName}
                   </param>
                   <!-- 下载的InputStream流,Struts2自己动对应Action中的getDownloadFile方法,该方法必须返回InputStream 类型 -->
               <param name="inputName">downloadFile</param>
           </result>
           <result name="operate">${nextPage}</result>
           <!-- result的Type必须为stream -->
           <result name="displayLog" type="stream">
                   <!-- 下载文件类型 -->
                   <param name="contentType">text/plain</param>
                   <!-- 下载对话框所弹出的文件名 -->
                   <param name="contentDisposition">
                    fileName=${currentLogName}
                   </param>
                   <!-- 下载的InputStream流,Struts2自己动对应Action中的getDownloadFile方法,该方法必须返回InputStream 类型 -->
               <param name="inputName">displayLog</param>
           </result>

 

注意1: contentDisposition 参数控制在浏览器中显示文件名等, attachment;指明文件以附件形式存在,即点link时会有一个提示框提示下载还是打开, 如果不配置attachment,那么默认是inline的,即浏览器会尝试直接打开文件. 其它参数用法直接看注释吧.

 

JAVA代码片段:

 

public String download(){
        System.out.println("download Log action requested!");
        return "download";
    }

public InputStream getDownloadFile() {
           ResourceBundle resource = ResourceBundle.getBundle("voicefileInfo");
           this.setLogDir(resource.getString("LogPath"));
           String tempfile;


           tempfile = resource.getString("LogDir").concat(this.getCurrentLogName());
          
           System.out.println("getDownloadFile()===>" + tempfile);
          
           return ServletActionContext.getServletContext().getResourceAsStream("/"+tempfile);
    }

 

    public String displayLog(){
        System.out.println("Display Log action requested!");
        return "displayLog";
    }

public InputStream getDisplayLog() {
           ResourceBundle resource = ResourceBundle.getBundle("voicefileInfo");
           this.setLogDir(resource.getString("LogPath"));
       
           String tempfile;

           tempfile = resource.getString("LogDir").concat(this.getCurrentLogName());
          
           System.out.println("getDownloadFile()===>" + tempfile);
          
           return ServletActionContext.getServletContext().getResourceAsStream("/"+tempfile);
    }

 

意外小插曲: java 流中的方法getResourceAsStream("...")只支持相对路径,不识别绝对路径...

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值