我在tomcat 8中部署了一个war。在其中一个jar中有abc.xml文件。现在,如果我试图在sample_jar的一个java类(Example.java)中的abc.xml上打开输入流,我将得到以下异常
Caused by: java.io.IOException: Stream closed
at java.util.zip.InflaterInputStream.ensureOpen(InflaterInputStream.java:67)
~[?:1.8.0_102]
at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:121)
~[?:1.8.0_102]
at org.apache.catalina.webresources.AbstractArchiveResource$JarInputStreamWrapper.read(AbstractArchiveResource.java:243)
~[catalina.jar:?]
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager$RewindableInputStream.read(XMLEntityManager.java:2890)
~[?:1.8.0_102]
at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:674)
~[?:1.8.0_102]
at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)
~[?:1.8.0_102]
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:805)
~[?:1.8.0_102]
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)
~[?:1.8.0_102]
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
~[?:1.8.0_102]
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
~[?:1.8.0_102]
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:643)
~[?:1.8.0_102]
at org.apache.commons.digester.Digester.parse(Digester.java:1892)
~[commons-digester-2.1.jar:2.1]
at net.sf.jasperreports.engine.xml.JRXmlLoader.loadXML(JRXmlLoader.java:298)
~[jasperreports-6.3.0.jar:6.3.0
现在奇怪的是它只是第一次发生(一旦战争部署和服务器启动),后续访问它运行正常。
我创建流的方式是 -
InputStream is= Example.class.getResourceAsStream("/abc.xml");
如果有人能指出错误,我将非常感激。
编辑-1 我正在访问文件的代码块 -
public class ReportManager {
public void pageSetting(Map<String, Object> map){
//code to set report's global info
}
public void createReport(String time) {
try(InputStream coverStream = ReportManager.class.getResourceAsStream("ReportCover");
InputStream reportStream = ReportManager.class.getResourceAsStream("ReportDetail")) {
JasperReport jasperReport1 = JasperCompileManager.compileReport(coverStream);
JasperReport jasperReport2 = JasperCompileManager.compileReport(reportStream);
Map<String, Object> templateParameters = new HashMap<String, Object>();
Map<String, Object> coverTemplateParameters = new HashMap<String, Object>();
pageSetting(coverTemplateParameters);
pageSetting(templateParameters);
JasperPrint jasperPrint1 = JasperFillManager.fillReport(jasperReport1, coverTemplateParameters,
new JREmptyDataSource());
JasperPrint jasperPrint2 = JasperFillManager.fillReport(jasperReport2, templateParameters,
new JREmptyDataSource());
List<JasperPrint> list = new ArrayList<>();
list.add(jasperPrint1);
list.add(jasperPrint2);
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(SimpleExporterInput.getInstance(list));
exporter.setExporterOutput(
new SimpleOutputStreamExporterOutput(System.getProperty("java.io.tmpdir") + File.separator + "Report_Output"+ time + ".pdf"));
exporter.exportReport();
} catch (JRException | GeneralException | IOException e2) {
logger.error("exception in report generation", e2);
}
}
}