【问题标题】:Struts 2 - HTTP Status 500 - Can not find a java.io.InputStream with the nameStruts 2 - HTTP 状态 500 - 找不到名称为 java.io.InputStream
【发布时间】:2014-07-11 14:09:20
【问题描述】:

这与以下问题类似:

Question A

Question B

但我无法在我的情况下应用解决方案。也许有人可以在我的代码中发现错误。

我想在我的 Struts2 应用程序中生成 Excel 文件并让用户下载它,但出现错误:

“HTTP 状态 500 - 找不到具有该名称的 java.io.InputStream”

通过调试器,我已经确认创建了带有文件内容的正确输入流,并且方法返回“成功”,因此 Struts 似乎没有拾取此流。

struts.xml 我有:

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

然后在ManageRefAction 班级我有:

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

    }

【问题讨论】:

  • 你有contentDisposition的getter吗?
  • 我没有 getter,但我正在设置 setContentDisposition("attachment; filename=" + exportFilename);
  • 它应该对结果可用,所以你必须创建一个getter。

标签: java struts2


【解决方案1】:

试试这个,contentDisposition 是字符串值没有进入 struts.xml。 所以,它抛出了一个异常。在参数中使用这个语法${contentDisposition}

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2011-01-01
    • 2014-01-08
    • 2013-02-23
    • 2012-12-04
    相关资源
    最近更新 更多