2

这是我的代码,我得到以下异常

HTTP Status 500 - Unable to show problem report: java.lang.IllegalStateException: getOutputStream() has already been called for this response

编码:

WorkbookSettings wbSettings = new WorkbookSettings();    
OutputStream outStream = null;
            
try
{               
  wbSettings.setLocale(new Locale("en", "EN"));             
  response.setContentType("application/vnd.ms-excel");                  
  outStream= response.getOutputStream();                
  response.setHeader("Content-Disposition", "attachment; filename=/timesheet.xls");             
  WritableWorkbook workbook = Workbook.createWorkbook(outStream, wbSettings);              
  workbook.createSheet("Report", 0);               
  WritableSheet excelSheet = workbook.getSheet(0);              
  service.createLabel(excelSheet);              
  service.createContent(excelSheet);                    
  workbook.write();             
  workbook.close();             
  outStream.flush();               
  outStream.close();                
}               
catch(Exception e)
{
}           
finally
{               
  //outStream.close();      
}   
return "generateReport";

我的Struts.xml样子是这样的:

<result type="stream" name="generateReport">                   
 <param name="contentType">"application/vnd.ms-excel"</param>                  
 <param name="inputName">excelstream</param>                  
 <param name="contentDisposition">contentDisposition</param>                   
 <param name="bufferSize">1024</param>              
</result>

在 JSP 中,我只是给了一个按钮,它给了我open, save对话框。单击该按钮后,我得到了异常。

如何避免这种情况?

4

4 回答 4

2

删除关闭 %> 和打开 <% 之间的所有空格和换行符并在顶部使用 <%@page trimDirectiveWhitespaces="true" %> 可以解决此问题。

于 2013-10-18T11:52:19.477 回答
2

完全删除关闭 %> 和打开 <% 之间的所有空格和换行符。在它们之间留有空间会导致 getOutputStream() 被自动调用。因为这些空格或换行符被视为浏览器的输出:它调用 getOutputStream() 来呈现它们。

这是我发现在 JSP 中解决此错误的唯一方法。否则,您将不得不重写将二进制文件返回为 servlet 的代码,并且只需将 JSP 页面用作启动页面,以便在用户单击按钮时将其发送到 servlet。

于 2013-09-06T22:57:34.720 回答
2

这只是一个语法错误,服务器混淆了如何处理这种内容类型

<param name="contentType">"application/vnd.ms-excel"</param>

改成

<param name="contentType">application/vnd.ms-excel</param>

注意,该param值是一个没有双引号的字符串。

所以,有效的结果是

<result type="stream" name="generateReport">
  <param name="contentType">application/vnd.ms-excel</param>
  <param name="contentDisposition">attachment;filename="timesheet.xls"</param>
  <param name="inputName">excelstream</param>
</result>

excelstream操作代码应在返回结果之前初始化并提供 getter。不应该写入响应,workbook让它写入ByteArrayOutputStream.

private InputStream excelstream;

public InputStream getExcelstream() {
  return excelstream;
}  

public String execute() throws Exception {
  WorkbookSettings wbSettings = new WorkbookSettings();

  try  {  
    ByteArrayOutputStream outstream = new ByteArrayOutputStream();  
    wbSettings.setLocale(new Locale("en", "EN"));        
    WritableWorkbook workbook = Workbook.createWorkbook(outstream, wbSettings);   
    workbook.createSheet("Report", 0);    
    WritableSheet excelSheet = workbook.getSheet(0);    
    service.createLabel(excelSheet);    
    service.createContent(excelSheet);          
    workbook.write();    
    workbook.close();  
    excelstream = new ByteArrayInputStream(outstream.toByteArray());        
  } catch(Exception e) {    
    e.printStackTrace();
    throw e;
  }

  return "generateReport";
}
于 2013-09-05T10:50:04.433 回答
1

你读过这个吗?

您需要inputSteam为参数设置一组, inputName但我在您的代码中的任何地方都看不到它。

您可以设置一个ByteArrayOutputStream并将数据存储到ByteArrayInputStream 如下所示

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

/** code to write to outputsteam ***/

ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());

而且您不需要包含这些代码

response.setContentType("application/vnd.ms-excel");    

outStream= response.getOutputStream();

response.setHeader("Content-Disposition", "attachment; filename=/timesheet.xls");

因为您可以在支撑动作结果中设置所有这些。

于 2012-12-28T06:47:48.163 回答