简体   繁体   English

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

[英]java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times

I am trying to read a file from aws s3 bucket and set it as resource inside my spring batch reader class. 我正在尝试从aws s3存储桶读取一个文件,并将其设置为我的spring batch reader类中的资源。 When I test the application on aws lambda function I got below error. 当我在AWS Lambda函数上测试应用程序时,出现以下错误。 any suggestion experts? 有什么建议专家吗?

    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 batch reader class 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);
    }
}

The docs suggest using ByteArrayResource to cache the content in memory, rather than InputStreamResource. 该文档建议使用ByteArrayResource将内容缓存在内存中,而不是InputStreamResource。

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/io/InputStreamResource.html 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 );

Instead of returning InputStreamResource , you shud return content of the stream may be byte[ ]. 您不必返回InputStreamResource,而是将流的返回内容设为byte []。
byte[] content = IOUtils.toByteArray(object.getObjectContent()); byte [] content = IOUtils.toByteArray(object.getObjectContent()); return content ; 返回内容;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException: 已经为此请求调用了getReader() - java.lang.IllegalStateException: getReader() has already been called for this request java.lang.IllegalStateException:已为此响应调用getWriter() - java.lang.IllegalStateException: getWriter() has already been called for this response java.lang.IllegalStateException:已经为此响应调用了 getOutputStream() - java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:已为此响应调用了getOutputStream() - java.lang.IllegalStateException: getOutputStream() has already been called for this response java.lang.IllegalStateException:此请求已调用getInputStream() - java.lang.IllegalStateException: getInputStream() has already been called for this request java.lang.IllegalStateException:有一个打开的流编写器时无法读取 - java.lang.IllegalStateException: Cannot read while there is an open stream writer java.io.IOException:超出流末尾写入,或者java.lang.IllegalStateException:getOutputStream()已被调用 - java.io.IOException: write beyond end of stream OR java.lang.IllegalStateException: getOutputStream() has already been called java.lang.IllegalStateException:文件已移动-无法在Mulipart文件上传中使用JavaMail重新读取Spring MVC - java.lang.IllegalStateException: File has been moved - cannot be read again in Mulipart file upload Spring MVC with JavaMail GCloud java 错误:java.lang.IllegalStateException:响应已提交 - GCloud java error: java.lang.IllegalStateException: Response has already been committed java.lang.IllegalStateException:在为当前请求调用 getReader() 后无法调用 getInputStream() - java.lang.IllegalStateException: Cannot call getInputStream() after getReader() has already been called for the current request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM