0

我在我的网络服务上收到太多打开文件错误,只是想确保没有其他需要关闭的东西。我添加了一个close()on outWriter( StringWriter),但 JavaDoc 说这没有效果。getCachedExtractSoapBodyXslt()获取javax.xml.transform.Transformer对象。

String payload;
StreamResult result=null;
StringWriter outWriter=null;
try {
    // Programmatically extract the SOAP Body from message
    outWriter = new StringWriter();
    result = new StreamResult(outWriter);
    Source src = new StreamSource(new java.io.StringReader(doc));
    getCachedExtractSoapBodyXslt().transform(src, result);
    StringBuffer sb = outWriter.getBuffer();
    payload = sb.toString();
} catch (TransformerException e) {
    throw new ComponentException("Unable to extract SOAP Body");
}
finally {
    LOG.debug("Closing XSLT transformer output stream");
    try {
        outWriter.close();
    }
    catch (IOException e) {
        LOG.error("Failed to close stream for SOAP message");
    }
}
4

2 回答 2

2

我不得不把它移到它自己的变量中并关闭它

new java.io.StringReader(doc)
于 2014-07-23T13:44:59.047 回答
0

StreamSource 没有关闭,我观察到它的用户不会关闭它所拥有的流或阅读器。并且 try-with-resource 也不是一个选项,因为它不是“可关闭的”。所以不得不去老学校并使用一个 try/finally 调用它的 getInputStream 和 GetReader 方法并将它们的结果传递给 IOUtils.closeQuietly

StreamSource source = null;
try {
   source = new StreamSource(inputStream);
   // Do stuff with source
}
finally {
   IOUtils.closeQuietly(source.getInputStream());
}

在我的例子中,我实际上不得不同时调用 getInputStream 和 getReader,因为我的源代码是以两种不同的方式构建的。

于 2016-12-01T22:57:30.883 回答