gpt4 book ai didi

java.lang.IllegalStateException : getOutputStream() has already been called for this response

转载 作者:行者123 更新时间:2023-12-02 04:21:21 26 4
gpt4 key购买 nike

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

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 中,我只是提供了一个按钮,它提供了打开、保存对话框。单击该按钮后,我收到异常。

如何避免这种情况?

最佳答案

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

<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";
}

关于java.lang.IllegalStateException : getOutputStream() has already been called for this response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14065561/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com