1

我正在尝试从 S3 存储桶中读取多个文件并使用 进行处理MultiResourceItemReader,我ArrayStoreException在执行以下行时得到

InputStreamResource[] resources = resourceList.toArray(new InputStreamResource[resourceList.size()]);

请在下面找到我的代码:

@Bean
public MultiResourceItemReader<String> multiResourceReader()
{
    MultiResourceItemReader<String> resourceItemReader = new MultiResourceItemReader<String>();
    String bucketName = "my-product";
    String key = "/products";

    List<InputStream> resourceList = s3Service.getFiles(bucketName, key);
    List<InputStreamResource> inputStreamResourceList = new ArrayList<>();
    for (InputStream s: resourceList) {
        inputStreamResourceList.add(new InputStreamResource(s));
    }

    InputStreamResource[] resources = resourceList.toArray(new InputStreamResource[inputStreamResourceList.size()]);

    resourceItemReader.setResources(resources);
    return resourceItemReader;
}

例外:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.batch.item.file.MultiResourceItemReader]: Factory method 'multiResourceItemReader' threw exception; nested exception is java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, org.springframework.core.io.InputStreamResource
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653)
        ... 64 common frames omitted
Caused by: java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, org.springframework.core.io.InputStreamResource

有人可以帮我解决这个问题。提前感谢您的帮助。谢谢。

4

0 回答 0