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

时间:2022-03-15 23:16:11

I am using Spring-batch-excel for reading a excel file in my new application. It is configured as a batch job and triggered using JobManager. Now i getting this error. InputStream MUST either support mark/reset, or be wrapped as a PushbackInputStream

我使用Spring-batch-excel在我的新应用程序中读取excel文件。它被配置为批处理作业并使用JobManager触发。现在我收到了这个错误。 InputStream必须支持mark / reset,或者包装为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].

Any please help me.

请帮助我。

5 个解决方案

#1


1  

From looking at spring-batch-excel sources :

从查看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");
    }

    [...]
}

This exception is thrown if the InputStream does not support back reading. The InputStream depends of your Resource, so my conclusion would be that your resource is not a valid XLS/XLSX file.

如果InputStream不支持反向读取,则抛出此异常。 InputStream取决于您的资源,因此我的结论是您的资源不是有效的XLS / XLSX文件。

#2


2  

Old question and I'm sure you figured it out, but I find the current answer to be unhelpful,sooo...

老问题,我相信你已经明白了,但我发现目前的答案是无益的,所以......

The Resource you're using could be an issue. Most spring-batch-excel examples utilize ClassPathResource. When you try to productionalize your code, you'll likely need to reach for files outside of your classpath. The obvious choice is FileSystemResource, but that will result in this Exception. Instead, look at UrlResource.

您正在使用的资源可能是一个问题。大多数spring-batch-excel示例都使用ClassPathResource。当您尝试生成代码时,您可能需要访问类路径之外的文件。显而易见的选择是FileSystemResource,但这将导致此异常。相反,请查看UrlResource。

#3


0  

I know this is an old issue , but i faced this issue, yet for those who have the same issue like me, i commented out this code of block and it worked

我知道这是一个老问题,但我遇到了这个问题,但对于那些像我一样有问题的人,我评论了这段代码并且它有效

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

to understand why please refer to this code : https://github.com/spring-projects/spring-batch-extensions/issues/34

要理解为什么请参考此代码:https://github.com/spring-projects/spring-batch-extensions/issues/34

#4


0  

I faced this issue today. The solution was to load the file from disk and not from the classpath. The interesting part is that on our local Windows environments loading from classpath worked fine, but not on DEV Unix environment (I believe @Andy Sampson alludes to this). In our case, we specify the location of excel to load via prefix like 'classpath:' or 'file:' in a URL query parameter. So all we had to do was put the file in some path on the same server as the running app, then update the URL query parameter to location like this:

我今天遇到了这个问题。解决方案是从磁盘加载文件而不是从类路径加载文件。有趣的是,在我们的本地Windows环境中,从classpath加载工作正常,但不是在DEV Unix环境中(我相信@Andy Sampson暗示这一点)。在我们的示例中,我们通过URL查询参数中的'classpath:'或'file:'前缀指定要加载的excel的位置。因此,我们所要做的就是将文件放在与正在运行的应用程序相同的服务器上的某个路径中,然后将URL查询参数更新为如下位置:

http://some.host.com?location=file:/absolute/path/to/excel/file

http://some.host.com?location=file:/absolute/path/to/excel/file

and voila. Internally we use Spring ResourceLoader, which is nothing more than the Spring ApplicationContext. We got a reference to that ResourceLoader simply by having our excel loading class/Spring @Service implement Spring's ResourceLoaderAware, which can understand 'classpath:' and 'file:' prefixes.

瞧。在内部我们使用Spring ResourceLoader,它只不过是Spring ApplicationContext。我们只需要通过我们的excel加载类/ Spring @Service实现Spring的ResourceLoaderAware来引用该ResourceLoader,它可以理解'classpath:'和'file:'前缀。

#5


0  

As @Thrax mentioned, spring-batch-excel expect to find a PushbackInputStream within the Resource. Here my solution to this problem when working with file on the filesystem:

正如@Thrax所提到的,spring-batch-excel期望在Resource中找到PushbackInputStream。这是我在文件系统上使用文件时解决这个问题的方法:

I create my reader using input file from command line --input.file

我使用命令行--input.file中的输入文件创建我的阅读器

@Bean
public PoiItemReader excelReader(@Value("${input.file}") String inputFile) throws FileNotFoundException {
    PoiItemReader reader = new PoiItemReader();
    PushbackInputStream input = new PushbackInputStream(new FileInputStream(inputFile));
    InputStreamResource resource = new InputStreamResource(input);
    reader.setResource(resource);
    reader.setRowMapper(rowMapper());
    return reader;
}

I hope it may helps you.

我希望它可以帮助你。

#1


1  

From looking at spring-batch-excel sources :

从查看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");
    }

    [...]
}

This exception is thrown if the InputStream does not support back reading. The InputStream depends of your Resource, so my conclusion would be that your resource is not a valid XLS/XLSX file.

如果InputStream不支持反向读取,则抛出此异常。 InputStream取决于您的资源,因此我的结论是您的资源不是有效的XLS / XLSX文件。

#2


2  

Old question and I'm sure you figured it out, but I find the current answer to be unhelpful,sooo...

老问题,我相信你已经明白了,但我发现目前的答案是无益的,所以......

The Resource you're using could be an issue. Most spring-batch-excel examples utilize ClassPathResource. When you try to productionalize your code, you'll likely need to reach for files outside of your classpath. The obvious choice is FileSystemResource, but that will result in this Exception. Instead, look at UrlResource.

您正在使用的资源可能是一个问题。大多数spring-batch-excel示例都使用ClassPathResource。当您尝试生成代码时,您可能需要访问类路径之外的文件。显而易见的选择是FileSystemResource,但这将导致此异常。相反,请查看UrlResource。

#3


0  

I know this is an old issue , but i faced this issue, yet for those who have the same issue like me, i commented out this code of block and it worked

我知道这是一个老问题,但我遇到了这个问题,但对于那些像我一样有问题的人,我评论了这段代码并且它有效

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

to understand why please refer to this code : https://github.com/spring-projects/spring-batch-extensions/issues/34

要理解为什么请参考此代码:https://github.com/spring-projects/spring-batch-extensions/issues/34

#4


0  

I faced this issue today. The solution was to load the file from disk and not from the classpath. The interesting part is that on our local Windows environments loading from classpath worked fine, but not on DEV Unix environment (I believe @Andy Sampson alludes to this). In our case, we specify the location of excel to load via prefix like 'classpath:' or 'file:' in a URL query parameter. So all we had to do was put the file in some path on the same server as the running app, then update the URL query parameter to location like this:

我今天遇到了这个问题。解决方案是从磁盘加载文件而不是从类路径加载文件。有趣的是,在我们的本地Windows环境中,从classpath加载工作正常,但不是在DEV Unix环境中(我相信@Andy Sampson暗示这一点)。在我们的示例中,我们通过URL查询参数中的'classpath:'或'file:'前缀指定要加载的excel的位置。因此,我们所要做的就是将文件放在与正在运行的应用程序相同的服务器上的某个路径中,然后将URL查询参数更新为如下位置:

http://some.host.com?location=file:/absolute/path/to/excel/file

http://some.host.com?location=file:/absolute/path/to/excel/file

and voila. Internally we use Spring ResourceLoader, which is nothing more than the Spring ApplicationContext. We got a reference to that ResourceLoader simply by having our excel loading class/Spring @Service implement Spring's ResourceLoaderAware, which can understand 'classpath:' and 'file:' prefixes.

瞧。在内部我们使用Spring ResourceLoader,它只不过是Spring ApplicationContext。我们只需要通过我们的excel加载类/ Spring @Service实现Spring的ResourceLoaderAware来引用该ResourceLoader,它可以理解'classpath:'和'file:'前缀。

#5


0  

As @Thrax mentioned, spring-batch-excel expect to find a PushbackInputStream within the Resource. Here my solution to this problem when working with file on the filesystem:

正如@Thrax所提到的,spring-batch-excel期望在Resource中找到PushbackInputStream。这是我在文件系统上使用文件时解决这个问题的方法:

I create my reader using input file from command line --input.file

我使用命令行--input.file中的输入文件创建我的阅读器

@Bean
public PoiItemReader excelReader(@Value("${input.file}") String inputFile) throws FileNotFoundException {
    PoiItemReader reader = new PoiItemReader();
    PushbackInputStream input = new PushbackInputStream(new FileInputStream(inputFile));
    InputStreamResource resource = new InputStreamResource(input);
    reader.setResource(resource);
    reader.setRowMapper(rowMapper());
    return reader;
}

I hope it may helps you.

我希望它可以帮助你。