---
title: "如何让Struts2.0下载文件流"
author: "tailsherry"
date: "2008-04-01T12:20:04Z"
tags: []
slug: "struts2-0"
draft: false
lastmod: "2008-04-01T12:20:04Z"
id: 178351
url: "https://tailsherry.iteye.com/blog/178351"
---

在实际web应用中，大部分文件下载都是通过url文件链接直接下载的，同样在Struts中也可以这样实现。但是考虑到盗链，跨服务器访问等因素，直接文件流下载也是必要的。那么，在Struts2.0中如何实现数据流下载呢？

Struts2.0默认支持多种格式的result type，stream即是其中的一种。如果我这里要实现一个Generate Report的功能，将Report存放在一个InputStream里面，Action的示例代码内容如下：

```
package com.test;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class ReportsAction extends ActionSupport {

    // 定义HTML类型的Report
    private static final int HTML_TYPE = 0;
    
    // 定义EXCEL类型的Report
    private static final int EXCEL_TYPE = 1;
    
    // Report类型
    private int reportType;
    
    // Report输出流
    public InputStream reportStream;

    // 输出流Content Type
    public String contentType;
    
    // 输出流的生成的文件名
    public String fileName;
    
    public ReportsAction() {        
    }

    public String getContentType() {
        return contentType;
    }

    public String getFileName() {
        return fileName;
    }

    public InputStream getReportStream() {
        return reportStream;
    }

    public int getReportType() {
        return reportType;
    }

    public void setReportType(int reportType) {
        this.reportType = reportType;
    }

    public String generateReport() {
        switch (reportType) {
        case HTML_TYPE:
            // 获取HTML流
            reportStream = service.getHtmlStream();
            // contentType为MIME定义的，详细的内容可参考下面的这个网站：http://www.w3schools.com/media/media_mimeref.asp
            contentType = "text/html";
            // inline表示文件直接输出到网页上，不出现保存打开对话框
            fileName = "inline; filename=\"Report.htm\"";
            break;
        case EXCEL_TYPE:
            // 获取EXCEL流
            reportStream = service.getExcelStream();
            // contentType设定
            contentType = "application/vnd.ms-excel";
            // attachment表示网页会出现保存、打开对话框
            fileName = "attachment; filename=\"Report.xls\"";
            break;
        default:
            ;
        }
        return SUCCESS;
    }
        
}
```

 

当然，Struts的配置也是非常重要的，如下：

```
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="root" namespace="/">
        <action name="generateReport" method="generateReport"
	  class="com.test.ReportsAction">
            	    <result name="success" type="stream">
                      <!-- 对应ReportsAction中的属性contentType -->
	        <param name="contentType">${contentType}</param>
                      <!-- ReportsAction中对应的InputStream的属性名 -->
	        <param name="inputName">reportStream</param>
                      <!-- 对应ReportsAction中的属性fileName，定义流输出格式 -->
	        <param name="contentDisposition">${fileName}</param>
                      <!-- 定义bufferSize，可选 -->
	        <param name="bufferSize">1024</param>
	    </result>
                  ...
        </action>
    </package>
</struts>
```

 

页面部分我就不详细写了，比如，可以在一个form的提交中绑定这个action，普通的网页调用代码如下：

```
<form id="generateReportForm" action="generateReport.action" method="POST">
</form>
```

 

当然，你也可以用一个Struts中的标签来实现，示例代码如下：

```
<s:form theme="simple" validate="true">
    <s:submit cssStyle="width:160px" action="generateReport" value="Generate HTML Report" />
    <s:url id="generateUrl" action="generateReport"></s:url>
    <s:a href="%{generateUrl}"><s:textfield name="tail.button.generatexls" /></s:a>
</s:form>
```