gpt4 book ai didi

java - 为什么当我开始读取时 InputStreamResource 关闭了?

转载 作者:可可西里 更新时间:2023-11-01 10:00:31 54 4
gpt4 key购买 nike

我有一个 MongoDB,它存储一个 csv 文件和两个微服务(Spring Boot 应用程序)。一个微服务处理文件上传并支持将这些文件作为流获取。另一个微服务处理文件。这些微服务是分开的,因为我以后可能想以不同于 MongoDB 的其他方式存储文件,并且我不想由此影响其他服务。

我的问题是,当文件服务将文件作为 InputStreamResource 返回时,如下所示:

@RequestMapping(value = "/upload/{importId}", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<InputStreamResource> getUploadedFile(@PathVariable("importId") String importId)
throws ImportFileNotFoundException {
GridFSDBFile file = FileImportClient.getInstance().getFile(importId);

return ResponseEntity
.ok()
.contentLength(file.getLength())
.body(new InputStreamResource(file.getInputStream()));
}

我想从已经关闭的流中读取。

ResponseEntity<InputStreamResource> response = restTemplate
.getForEntity("http://upload-service/upload/" + importId, InputStreamResource.class);
BufferedReader fileReader = new BufferedReader(new InputStreamReader(response.getBody().getInputStream()));
for (CSVRecord record : CSVFormat.DEFAULT.parse(fileReader)) {/*toStuff()*/}

我得到一个 java.io.IOException: Stream closed由解析方法引起。

如果我只是 curl localhost:<port>/upload/<importId>我得到了整个 csv 文件。有谁知道为什么 InputStream 关​​闭以及如何解决?

堆栈跟踪:

java.io.IOException: Stream closed
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:170)
at java.io.BufferedInputStream.read(BufferedInputStream.java:336)
at java.io.FilterInputStream.read(FilterInputStream.java:133)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.read1(BufferedReader.java:210)
at java.io.BufferedReader.read(BufferedReader.java:286)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.read(BufferedReader.java:182)
at org.apache.commons.csv.ExtendedBufferedReader.read(ExtendedBufferedReader.java:60)
at org.apache.commons.csv.Lexer.nextToken(Lexer.java:89)
at org.apache.commons.csv.CSVParser.nextRecord(CSVParser.java:498)
at org.apache.commons.csv.CSVParser$1.getNextRecord(CSVParser.java:439)
at org.apache.commons.csv.CSVParser$1.hasNext(CSVParser.java:452)
at <mypackage>.<myclass>.process(<myclass>.java:71)

最佳答案

我通过此链接找到了解决方案:https://jira.spring.io/browse/SPR-7357

[This answers my question] Note that you cannot simply return the InputStream from the extractor, because by the time the execute method returns, the underlying connection and stream are already closed

[This fixes the problem] You can already read an InputStream by using the execute() method on the RestTemplate in combination with a ResponseExtractor implementation

我尝试使用 ResponseExtractor,现在可以正常使用了。它看起来像这样:

return restTemplate.execute(new URI("http://<myservice>/upload/" + importId), HttpMethod.GET, null,
new ResponseExtractor<MyObject>() {
@Override
public MyObject extractData(ClientHttpResponse response) throws IOException {
CSVParser parse = CSVFormat.DEFAULT.parse(new BufferedReader(new InputStreamReader(response.getBody())));
for (CSVRecord record : parse) {/*dostuff*/}
return new MyObject();
}
});

关于java - 为什么当我开始读取时 InputStreamResource 关闭了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35292207/

54 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com