MongoDB支持将二进制文件存储在其文件系统GridFS中。 Spring Data MongoDB 提供了一个 and 接口以及相应的实现,以及 ,让你与文件系统进行交互。 您可以通过给它一个 / 和 一个 来设置模板实例,如以下示例所示:GridFsOperationsReactiveGridFsOperationsGridFsTemplateReactiveGridFsTemplateMongoDatabaseFactoryReactiveMongoDatabaseFactoryMongoConverterSpring中文文档

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中文文档

使用 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(…)InputStreamMongoConverterGridFsTemplateDocumentSpring中文文档

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>MongoConverterReactiveGridFsTemplateDocumentSpring中文文档

MongoDB的驱动程序使用和接口来交换二进制流。 Spring Data MongoDB 将这些接口调整为 . 在 Spring 的参考文档中阅读更多相关信息。AsyncInputStreamAsyncOutputStreamPublisher<DataBuffer>DataBufferSpring中文文档

您可以通过 或 方法从文件系统中读取文件。 让我们先来看看方法。 您可以查找与 . 可以使用帮助程序类来定义查询。 它提供了静态工厂方法来封装默认元数据字段(如 和 )或自定义元数据字段。 以下示例演示如何使用模板查询文件:find(…)getResources(…)find(…)QueryGridFsCriteriawhereFilename()whereContentType()whereMetaData()Spring中文文档

使用 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 检索文件时定义排序条件。因此,在传递到方法中的实例上定义的任何排序条件都将被忽略。Queryfind(…)
目前,MongoDB 不支持在从 GridFS 检索文件时定义排序条件。因此,在传递到方法中的实例上定义的任何排序条件都将被忽略。Queryfind(…)

从 GridFs 读取文件的另一个选项是使用接口引入的方法。 它们允许将 Ant 路径传递到方法中,因此可以检索与给定模式匹配的文件。 以下示例演示如何使用读取文件:ResourcePatternResolverGridFsTemplateSpring中文文档

使用 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扩展并允许(例如)插入到从MongoDB数据库中读取Spring Config文件。ResourcePatternResolverGridFsTemplateApplicationContextSpring中文文档

默认情况下,在第一次 GridFS 交互时获取一次。 之后,模板实例将重用缓存的存储桶。 若要使用不同的存储桶,请从同一 Template 实例使用接受 .GridFsTemplateGridFSBucketSupplier<GridFSBucket>
默认情况下,在第一次 GridFS 交互时获取一次。 之后,模板实例将重用缓存的存储桶。 若要使用不同的存储桶,请从同一 Template 实例使用接受 .GridFsTemplateGridFSBucketSupplier<GridFSBucket>