excelStreamI am trying to download an excel file.
In my Action class
public class ActivityTrackerExlReportAction extends BaseAction
{
private InputStream excelStream;
private UserMasterDTO userMasterDTO;
public InputStream getExcelStream()
{
return excelStream;
}
public void setExcelStream(InputStream excelStream) {
this.excelStream = excelStream;
}
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
WorkbookSettings wbSettings = new WorkbookSettings();
try
{
response.setHeader("Content-Disposition", "attachment; filename=/timesheet.xls");
wbSettings.setLocale(new Locale("en", "EN"));
WritableWorkbook workbook = Workbook.createWorkbook(outputStream, wbSettings);
workbook.createSheet("Report", 0);
WritableSheet excelSheet = workbook.getSheet(0);
service.createLabel(excelSheet);
service.createContent(excelSheet);
workbook.write();
ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
setExcelStream(inputStream);
workbook.close();
outStream.flush();
outStream.close();
}
catch(Exception e)
{
}
finally
{
// outStream.close();
}
return "generateReport
}
My struts.xml
contains:
<result type="stream" name="generateReport">
<param name="contentType">"application/vnd.ms-excel"</param>
<param name="inputName">excelStream</param>
<param name="bufferSize">1024</param>
</result>
I am using JXL
to create and write an Excel sheet. Why am i getting the error and how to get out of it? Can not find a java.io.InputStream
with the name [excelStream]
in the invocation stack
My are Stacktraces:
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with the name [excelStream] in the invocation stack. Check the tag specified for this action.
org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:237)
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:186) .......
EDIT
In the new posted code, you are doing two "bad" things:
1) You are swallowing an exception, that is really really wrong;
put a
e.printStackTrace();
inside your
catch(Exception e){}
block, and you will see the Exception that is probably being throwed, catched and not showed;
2) You are returning the same result even if you get an Exception, (that is 99.9% what is happening); this will result in Struts2 Stream result trying to reach the excelStream variable that was never initialized (because of the exception).
In case of error, you should return an global (or local) error result type, with a JSP showing the error, instead of a Stream result type.
Print the exception, correct the code, and then everything will be alright :)
P.S: Please avoid writing in the response directly, use contentDisposition from the Stream result type.
The error is
<param name="inputName">excelstream</param>
should be
<param name="inputName">excelStream</param>
Always use camelCase (as you did in the Action, but not in struts config).
And obviously contentDisposition should be removed, or set with a proper value, like
<param name="contentDisposition">attachment; filename="myExcel.xls"</param>