struts 2 Can not find a java.io.InputStream with the name [excelStream] in the invocation stack

20,481

Solution 1

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>

Solution 2

The reason why you got this problem is that:

when struts2 want to get inputstream, it will call getExcelStream() method to get the stream, but the returned InputStream is null.

The solution is that: Open the file and return a new InputStream in getExcelStream()

Share:
20,481
Akhilesh N
Author by

Akhilesh N

Updated on July 09, 2022

Comments

  • Akhilesh N
    Akhilesh N almost 2 years

    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) .......