1

This is similar problem to:

Question A

Question B

yet I can't apply solutions in my case. Maybe someone can spot error in my code.

I want to generate Excel file in my Struts2 app and let user download it, but I get an error:

"HTTP Status 500 - Can not find a java.io.InputStream with the name"

With debugger I have confirmed that proper input stream with file content is created and method returns 'Success', so it seems that Struts is not picking up this stream.

In struts.xml I have:

    <action name="exportReferences" class="manageRefAction" method="exportReferences">
        <result name="success" type="stream">
            <param name="contentType">application/vnd.ms-excel;charset=utf-8</param>
            <param name="contentDisposition">contentDisposition</param>
            <param name="inputName">excelStream</param>
            <param name="bufferSize">1024</param>
        </result>
            (...)
    </action>   

Then in ManageRefAction class I have:

private InputStream excelStream;

  public InputStream getExcelStream() {
        return this.excelStream;
    }

 public String exportReferences() {

        List<ReferenceData> referenceXLSList = null;
        String exportFilename = null;
        String filename = "";
        String retrunStr = process();

        Map session = (Map) ActionContext.getContext().get("session");

        if (retrunStr.equalsIgnoreCase(ActionSupport.LOGIN)) {
            setLog("2");

            return LOGIN;
        } else {
            //Export only data with user's locale
            ArrayList userLocale = (ArrayList) session.get("userLocale");
            List<ArrayList> list = Arrays.asList(userLocale);
            try {
                referenceXLSList = referenceDataService.fetchReferenceDataXls();
                // Data filtering
                for (Iterator<ReferenceData> iterator = referenceXLSList.iterator(); iterator.hasNext(); ) {
                    ReferenceData ref = iterator.next();
                    String l = ref.getLocal().toLowerCase();
                    if (!userLocale.contains(l)) {
                        iterator.remove();
                    }
                }
                if (referenceXLSList.isEmpty()) {
                    return INPUT;
                }
                ServletContext servletContext = ServletActionContext.getServletContext();
                String exportFolderPath = servletContext.getRealPath("/files" + "/xls");

                File folder = new File(exportFolderPath);
                // Creation of the folder if it is not existing
                if (!folder.exists())
                    folder.mkdirs();

                exportFilename = PLConstants.REF_SHEET_NAME + PLConstants.XLS_FILE_EXTENSION;
                filename = exportFolderPath + "/" + exportFilename;
                File file = new File(filename);
                file.createNewFile();
                ExcelWriter writer = new ExcelWriter();
                Workbook wb = writer.writeRefData(filename, exportFilename, referenceXLSList);
                setContentDisposition("attachment; filename=" + exportFilename);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                wb.write(baos);
                this.excelStream = new ByteArrayInputStream(baos.toByteArray());
                return SUCCESS;
            } catch (Throwable e) {
                e.printStackTrace();
                logger.info("Exception while exporting partner references", e);
                return INPUT;
            }
        }

    }
Community
  • 1
  • 1
Michlis
  • 107
  • 11

1 Answers1

1

Try this, contentDisposition is String value not getting in struts.xml. so,it's throwing an exception.use this syntax ${contentDisposition} in param

    <action name="exportReferences" class="manageRefAction" method="exportReferences">
    <result name="success" type="stream">
        <param name="contentType">application/vnd.ms-excel;charset=utf-8</param>
        <param name="contentDisposition">${contentDisposition}</param>
        <param name="inputName">excelStream</param>
        <param name="bufferSize">1024</param>
    </result>
        (...)
  </action>
hari
  • 1,874
  • 1
  • 16
  • 10