使用Spring-batch-excel读取Excel时出错

我正在使用Spring-batch-excel来读取我的新应用程序中的excel文件。 它被configuration为批处理作业,并使用JobManager进行触发。 现在我得到这个错误。 InputStream必须支持标记/重置,或者封装为PushbackInputStream

Caused by: java.lang.IllegalStateException: InputStream MUST either support mark/reset, or be wrapped as a PushbackInputStream at org.springframework.batch.item.excel.poi.PoiItemReader.openExcelFile(PoiItemReader.java:82) ~[spring-batch-excel-0.5.0-SNAPSHOT.jar:?] at org.springframework.batch.item.excel.AbstractExcelItemReader.doOpen(AbstractExcelItemReader.java:111) ~[spring-batch-excel-0.5.0-SNAPSHOT.jar:?] at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:144) ~[spring-batch-infrastructure-3.0.5.RELEASE.jar:3.0.5.RELEASE]. 

任何请帮助我。

从查看spring-batch-excel来源:

 @Override protected void openExcelFile(final Resource resource) throws Exception { workbookStream = resource.getInputStream(); if (!workbookStream.markSupported() && !(workbookStream instanceof PushbackInputStream)) { throw new IllegalStateException("InputStream MUST either support mark/reset, or be wrapped as a PushbackInputStream"); } [...] } 

如果InputStream不支持反向读取,则会引发此exception。 InputStream取决于您的Resource ,所以我的结论是您的资源不是有效的XLS / XLSX文件。

老问题,我相信你知道了,但我觉得目前的答案是无益的,所以…

您正在使用的Resource可能是一个问题。 大多数spring-batch-excel示例使用ClassPathResource 。 当您尝试生产您的代码时,您可能需要到达类path之外的文件。 FileSystemResource明显的select,但是这将导致这个exception。 相反,看看UrlResource

我知道这是一个老问题,但我面临这个问题,但对于像我这样的问题,我注释了这个代码块,它的工作

 if (!workbookStream.markSupported() && !(workbookStream instanceof PushbackInputStream)) { throw new IllegalStateException("InputStream MUST either support mark/reset, or be wrapped as a PushbackInputStream"); } 

了解为什么请参考下面的代码: https : //github.com/spring-projects/spring-batch-extensions/issues/34