【问题标题】:what closes OutputStream in AbstractSerializer?什么在 AbstractSerializer 中关闭 OutputStream?
【发布时间】:2014-03-12 16:08:47
【问题描述】:

我有自己的课程扩展 org.apache.cocoon.serialization.AbstractSerializer (很基础)

public class ExcelSerializer extends AbstractSerializer {

private static final XLogger LOG = XLoggerFactory.getXLogger(ExcelSerializer.class);

private ExcelSheetCreator excelSheetCreator;

public ExcelSerializer() {
    try {
        excelSheetCreator = new ExcelSheetCreator();
    } catch (IOException e) {
        LOG.error("ERROR", e.getMessage());
    }
}



@Override
public void startElement(String uri, String loc, String raw, Attributes a) throws SAXException {
    excelSheetCreator.startElement(uri, loc, raw, a);
}

@Override
public void characters(char[] c, int start, int len) throws SAXException {
    excelSheetCreator.characters(c, start, len);
}

@Override
public void endElement(String uri, String loc, String raw) throws SAXException {
    excelSheetCreator.endElement(uri, loc, raw);
}


@Override
public void endDocument() throws SAXException {
    excelSheetCreator.setOutputStream(this.output);
    excelSheetCreator.endDocument();

}

}

第一次尝试一切正常,我得到了预期的输出。从第二个开始,我从 ExcelSerializer 调用的类抛出 IOException,因为流已经关闭。

@Override
public void endDocument() throws SAXException {
    try {
        workbook.write(outputStream);
    } catch (IOException e) {
        System.out.println("workbook");
        System.out.println("" + e.getMessage());
    }
    workbook.dispose();
}

我当然没有关闭输出流,或者至少没有故意关闭。 我该怎么做才能让它保持打开状态?

这是我的站点地图:

 <map:serializer  name="excelSerializer" logger="sitemap.serializer.excelSerializer" src="com.acrys.excel.ExcelSerializer" 
           mime-type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">
         </map:serializer>

【问题讨论】:

    标签: java xmlserializer outputstream


    【解决方案1】:

    原来工作簿的某些内部输出流已关闭,而不是 ExcelSerializer 类中的输出流。一旦 workbook.dispose() 被调用,工作簿就不能再做任何工作了。解决方案是在每次通话时创建一个新工作簿。

    【讨论】:

      猜你喜欢
      • 2012-03-30
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2018-02-25
      • 1970-01-01
      • 2023-03-13
      • 1970-01-01
      • 2013-03-29
      相关资源
      最近更新 更多