---
title: "STUTS2 下载 getOutputStream() has already been called for this response异常的原因"
author: "CHEEN"
date: "2012-09-08T10:10:36Z"
tags: []
categories: ["编程语言"]
slug: "stuts2-getoutputstream-has-already-been-called-for-this-response"
draft: false
lastmod: "2012-09-08T10:15:57Z"
id: 1673922
url: "https://cheen.iteye.com/blog/1673922"
---

```java
public String excel() throws Exception{
		initRequest();
		//查询ID
		String queryId = request.getParameter("queryId");
		RequestParamters requestParamters = new RequestParamters(request, false);
		HSSFWorkbook wb = null;
		try {
			wb = publicSrv.excel(queryId, getSqlColumns(queryId), requestParamters);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		HttpServletResponse response = getResponse();
		OutputStream os = null;
		try {
			QueryConfig queryConfig = ConfigQuery.getQueryConfig(queryId);
			String title = null;
			if(null != queryConfig){
				title = queryConfig.getExcelTitle();
			}
			if(StrUtils.isEmpty(title)){
				title = "无标题";
			}
			os = response.getOutputStream();// 取得输出流
			response.reset();// 清空输出流
			response.setHeader("Content-disposition","attachment;filename="+new String((title).getBytes(),"ISO8859_1")+".xls");// 设定输出文件头
			response.setContentType("application/msexcel");// 定义输出类型
			wb.write(os);
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			try {
				os.flush();
				os.close();
			} catch (IOException e) {}
			
		}
		return null;
	}
```

如果action的返回写成
```java
return SUCCESS;
```
在tomcat6.0.14的版本会出现
```java
getOutputStream() has already been called for this response
```
，在tomcat6.0.28中没有，解决办法是action最后必须是
```java
return null;
```