java.lang.IllegalStateException:已读取InputStream-如果需要多次读取流,请不要使用InputStreamResource

时间:2018-11-16 20:08:03

标签: java spring-boot aws-lambda spring-batch

我正在尝试从aws s3存储桶读取一个文件,并将其设置为我的spring batch reader类中的资源。当我在AWS Lambda函数上测试应用程序时,出现以下错误。有什么建议专家吗?

    Caused by: java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times
    at org.springframework.core.io.InputStreamResource.getInputStream(InputStreamResource.java:97) ~[task/:na]
    at org.springframework.batch.item.file.DefaultBufferedReaderFactory.create(DefaultBufferedReaderFactory.java:34) ~[task/:na]
    at org.springframework.batch.item.file.FlatFileItemReader.doOpen(FlatFileItemReader.java:266) ~[task/:na]
    at org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader.open(AbstractItemCountingItemStreamItemReader.java:146) ~[task/:na]

Class to read from s3 bucket
@Service
public class S3BucketProcessing {
private static final AmazonS3 s3 = AmazonS3ClientBuilder.standard().build();

public InputStreamResource readFile() throws IOException{

   String bucketName = "mybuckey";
   String key = "File.txt";

   S3Object object = s3.getObject(new GetObjectRequest(bucketName, key));    

   return new InputStreamResource(object.getObjectContent());

}

Spring批处理读取器类

    @Component
public class MyReader extends FlatFileItemReader<MyEntity> {

    MyLineMapper mapper;
    MyTokenizer tokenizer;
    S3BucketProcessing s3BucketProcessing;

    @Autowired
    public MyReader(MyTokenizer tokenizer, MyLineMapper mapper, S3BucketProcessing s3BucketProcessing) throws Exception{
        LOG.info("CardCustomerNotificationReader constructor");
        this.mapper = mapper;
        this.tokenizer = tokenizer;
        this.s3BucketProcessing= s3BucketProcessing;
        this.setResource(s3BucketProcessing.readFile());
        mapper.setLineTokenizer(tokenizer);
        this.setLineMapper(mapper);
    }
}

3 个答案:

答案 0 :(得分:2)

The docs suggest using ByteArrayResource to cache the content in memory, rather than InputStreamResource.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/InputStreamResource.html

Just change the returns part like this:

//As suggested by berzerk
byte[] content = IOUtils.toByteArray(object.getObjectContent()); 

//Then
return new ByteArrayResource( content );

答案 1 :(得分:1)

您无需返回InputStreamResource,而是将流的返回内容设为byte []。
byte [] content = IOUtils.toByteArray(object.getObjectContent());    返回内容;

答案 2 :(得分:0)

所以我不得不发送JSON / XML对象作为Output Stream。我正在使用InputStreamResource,并且得到与OP相同的错误。

这是对我有用的解决方案。

    @Override
    public Resource dataExportForFieldExtractorModel() {

        ObjectMapper xmlMapper = new XmlMapper().enable(SerializationFeature.INDENT_OUTPUT);
        byte[] data;
        Resource resource = null;
        try {
            data = xmlMapper.writerWithDefaultPrettyPrinter().writeValueAsBytes(new DataExportResponse());
            resource = new ByteArrayResource(data);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resource;
    }

由于资源需要为byte[],因此我将resource = new InputStreamResource(new ByteArrayInputStream(data));更改为 resource = new ByteArrayResource(data);