[英]GridFSDBFile in spring boot 2.0.1 release
我正在嘗試將spring boot 1.5.7遷移到其最新版本的realease 2.0.1。 在舊版本中,我使用此語法來恢復文件。
GridFSDBFile gridFsdbFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(fileId)));
你知道在新版本中會怎么做嗎? 我需要
file.setInputStream(gridFsdbFile.getInputStream());
TY!
GridFSFile gridFsdbFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFsdbFile.getObjectId());
GridFsResource gridFsResource = new GridFsResource(gridFsdbFile,gridFSDownloadStream );
然后
file.setInputStream(gridFsResource.getInputStream());
您可以像這樣在MongoConfiguration中定義gridFSBucket
@Bean public GridFSBucket getGridFSBuckets() {
MongoDatabase db = mongoDbFactory().getDb();
return GridFSBuckets.create(db);
}
讓它保持不變的臨時解決方案是包含依賴關系
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.10.11.RELEASE</version>
</dependency>
https://mvnrepository.com/artifact/org.springframework.data/spring-data-mongodb/1.10.11.RELEASE
似乎自Spring Boot 2.0.x GridFS操作以來一直在尋求資源方法。
鑒於GridFS中有唯一的文件名,您可以這樣做:
gridFsTemplate.getResource("my-file-name.txt")
可以使用任何仲裁ID代替真實文件名,以確保其在存儲文件時的唯一性(例如ObjectId)。
gridFsTemplate.store(inputStream, "5a55f1ea29485119f7a671a7")
另外在2.1.0.M3中提交了使用幫助器方法來獲取GridFSFile的資源,但是在引擎蓋下它仍然使用文件名 。
為了通過純ID獲取文件內容,您可以使用以下內容:
GridFSBuckets.create(dbFactory.getDb()).openDownloadStream(gridFsFileId)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.