Here is my code for which I am getting the following exception
HTTP Status 500 - Unable to show problem report: java.lang.IllegalStateException: getOutputStream() has already been called for this response
The code:
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";
My Struts.xml
looks like this:
<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>
In JSP, I am just giving a button which gives me open, save
dialog box. After clicking that button I am getting the exception.
How to avoid this?
It's just a syntax error, the server in confuse how to handle such content type
<param name="contentType">"application/vnd.ms-excel"</param>
change to
<param name="contentType">application/vnd.ms-excel</param>
note, the param
value is a string without double quotes.
So, the valid result would be
<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>
the action code should initialize the excelstream
and provide getter before returning result. The workbook
shouldn't write to the response, let it write to the 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";
}
Remove absolutely all space and linebreaks between closing %> and opening <%. Having space between those can cause getOutputStream() to be called automatically. Because those spaces or linebreaks are being treated as output to the browser: it calls getOutputStream() to render them.
Its the only way I've found to solve this error in a JSP. Otherwise you'll have to rewrite the code that returns the binary file to be a servlet and just use the JSP page as a launch page to send the user to the servlet when they click the button.
删除关闭 %> 和打开 <% 之间的所有空格和换行符,并在顶部使用 <%@page trimDirectiveWhitespaces="true" %> 可以解决此问题。
You need to have a inputSteam
set for the inputName
paramater but I cannot see it anywhere in your code.
You can set up a ByteArrayOutputStream
and store the data to ByteArrayInputStream
like below
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
/** code to write to outputsteam ***/
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
And you do not need to include these codes
response.setContentType("application/vnd.ms-excel");
outStream= response.getOutputStream();
response.setHeader("Content-Disposition", "attachment; filename=/timesheet.xls");
as you can set all these in the strut action result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.