对于最新的稳定版本,请使用 Spring Data MongoDB 4.5.2spring-doc.cadn.net.cn

GridFS 支持

MongoDB 支持在其文件系统 GridFS 中存储二进制文件。 Spring Data MongoDB 提供了一个GridFsOperationsReactiveGridFsOperations接口以及相应的实现,GridFsTemplateReactiveGridFsTemplate,让您与文件系统进行交互。 您可以通过向模板实例提供MongoDatabaseFactory/ReactiveMongoDatabaseFactory以及MongoConverter,如以下示例所示:spring-doc.cadn.net.cn

class GridFsConfiguration extends AbstractMongoClientConfiguration {

  // … further configuration omitted

  @Bean
  public GridFsTemplate gridFsTemplate() {
    return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter());
  }
}
class ReactiveGridFsConfiguration extends AbstractReactiveMongoConfiguration {

  // … further configuration omitted

  @Bean
  public ReactiveGridFsTemplate reactiveGridFsTemplate() {
    return new ReactiveGridFsTemplate(reactiveMongoDbFactory(), mappingMongoConverter());
  }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/data/mongo
https://www.springframework.org/schema/data/mongo/spring-mongo.xsd
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">

  <mongo:db-factory id="mongoDbFactory" dbname="database" />
  <mongo:mapping-converter id="converter" />

  <bean class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
    <constructor-arg ref="mongoDbFactory" />
    <constructor-arg ref="converter" />
  </bean>

</beans>

现在可以注入模板并用于执行存储和检索作,如以下示例所示:spring-doc.cadn.net.cn

使用 GridFS 存储文件
class GridFsClient {

  @Autowired
  GridFsOperations operations;

  @Test
  public void storeFileToGridFs() {

    FileMetadata metadata = new FileMetadata();
    // populate metadata
    Resource file = … // lookup File or Resource

    operations.store(file.getInputStream(), "filename.txt", metadata);
  }
}

store(…)作采用InputStream、文件名和(可选)有关要存储的文件的元数据信息。 元数据可以是任意对象,该对象将由MongoConverter配置为GridFsTemplate. 或者,您也可以提供Document.spring-doc.cadn.net.cn

class ReactiveGridFsClient {

  @Autowired
  ReactiveGridFsTemplate operations;

  @Test
  public Mono<ObjectId> storeFileToGridFs() {

    FileMetadata metadata = new FileMetadata();
    // populate metadata
    Publisher<DataBuffer> file = … // lookup File or Resource

    return operations.store(file, "filename.txt", metadata);
  }
}

store(…)作采用Publisher<DataBuffer>、文件名和(可选)有关要存储的文件的元数据信息。 元数据可以是任意对象,该对象将由MongoConverter配置为ReactiveGridFsTemplate. 或者,您也可以提供Document.spring-doc.cadn.net.cn

MongoDB的驱动程序使用AsyncInputStreamAsyncOutputStream交换二进制流的接口。 Spring Data MongoDB 将这些接口调整为Publisher<DataBuffer>. 阅读更多DataBufferSpring 的参考文档中。spring-doc.cadn.net.cn

您可以通过find(…)getResources(…)方法。 让我们来看看find(…)方法。 您可以找到与Query. 您可以使用GridFsCriteriahelper 类来定义查询。 它提供了静态工厂方法来封装默认元数据字段(例如whereFilename()whereContentType()) 或通过whereMetaData(). 以下示例演示如何使用模板查询文件:spring-doc.cadn.net.cn

使用 GridFsTemplate 查询文件
class GridFsClient {

  @Autowired
  GridFsOperations operations;

  @Test
  public void findFilesInGridFs() {
    GridFSFindIterable result = operations.find(query(whereFilename().is("filename.txt")));
  }
}
class ReactiveGridFsClient {

  @Autowired
  ReactiveGridFsTemplate operations;

  @Test
  public Flux<GridFSFile> findFilesInGridFs() {
    return operations.find(query(whereFilename().is("filename.txt")))
  }
}
目前,MongoDB 不支持在从 GridFS 检索文件时定义排序标准。因此,在Query实例交到find(…)方法被忽略。

从 GridF 读取文件的另一个选项是使用ResourcePatternResolver接口。 它们允许将 Ant 路径传递到方法中,从而可以检索与给定模式匹配的文件。 以下示例演示如何使用GridFsTemplate要读取文件:spring-doc.cadn.net.cn

使用 GridFsTemplate 读取文件
class GridFsClient {

  @Autowired
  GridFsOperations operations;

  public GridFsResources[] readFilesFromGridFs() {
     return operations.getResources("*.txt");
  }
}
class ReactiveGridFsClient {

  @Autowired
  ReactiveGridFsOperations operations;

  public Flux<ReactiveGridFsResource> readFilesFromGridFs() {
     return operations.getResources("*.txt");
  }
}

GridFsOperations延伸ResourcePatternResolver并让GridFsTemplate(例如)插入ApplicationContext从 MongoDB 数据库读取 Spring 配置文件。spring-doc.cadn.net.cn

默认情况下,GridFsTemplate获得GridFSBucket第一次 GridFS 交互时。 之后,模板实例将重用缓存的存储桶。 要使用不同的存储桶,请从同一个 Template 实例中使用Supplier<GridFSBucket>.