我正在尝试将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);
}