zoukankan      html  css  js  c++  java
  • 分布式文件系统----GridFS

    GridFS研究

      GridFS是MongoDB提供的用于持久化存储文件的模块,CMS使用MongoDB存储数据,使用GridFS可以快速集成开发。
    它的工作原理是:
      在GridFS存储文件是将文件分块存储,文件会按照256KB的大小分割成多个块进行存储,GridFS使用两个集合(collection)存储文件,一个集合是chunks,用于存储文件的二进制数据;一个集合是files,用于存储文件的元数据信息(文件名称、块大小、上传时间等信息)。从GridFS中读取文件要对文件的各各块进行组装、合并。

     

    详细参考:https://docs.mongodb.com/manual/core/gridfs/

    使用

    1、添加依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-mongodb</artifactId>
            </dependency>
    

    2、上传文件

        @Autowired
        GridFsTemplate gridFsTemplate;
        //存文件
        @Test
        public void testStore() throws FileNotFoundException {
            //定义file
            File file =new File("D:\a.txt");
            //定义fileInputStream
            FileInputStream fileInputStream = new FileInputStream(file);
            ObjectId objectId = gridFsTemplate.store(fileInputStream, "a.txt");
            //5dc6e1cf28763b92ec445496
            //objectId是fs.files集合的_id字段,是fs.chunks集合的fils_id字段
            System.out.println(objectId);
        }
    

    2、读取文件

      配置

    @Configuration
    public class MongoConfig {
        @Value("${spring.data.mongodb.database}")
        String db;
    
        @Bean
        public GridFSBucket getGridFSBucket(MongoClient mongoClient){
            MongoDatabase database = mongoClient.getDatabase(db);
            GridFSBucket bucket = GridFSBuckets.create(database);
            return bucket;
        }
    }
    

      application.yml

    server:
      port: 31001
    spring:
      application:
        name: xc-service-manage-cms
      data:
        mongodb:
          uri: mongodb://root:123@localhost:27017
          database: xc_cms

      使用 

    @Autowired
        GridFSBucket gridFSBucket;
        //取文件
        @Test
        public void queryFile() throws IOException {
            //根据文件id查询文件
            GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is("5dc6e1cf28763b92ec445496")));
    
            //打开一个下载流对象
            GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
            //创建GridFsResource对象,获取流
            GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFSDownloadStream);
            //从流中取数据
            String content = IOUtils.toString(gridFsResource.getInputStream(), "utf-8");
            System.out.println(content);
        }

     3、删除文件

        @Test
        public void testDelFile() throws IOException {
            //根据文件id删除fs.files和fs.chunks中的记录
            gridFsTemplate.delete(Query.query(Criteria.where("_id").is("5dc6e1cf28763b92ec445496")));
        }
    
  • 相关阅读:
    Mysql任务调度
    使用 IntraWeb (18)
    使用 IntraWeb (17)
    替盛大代发的招聘启示
    使用 IntraWeb (16)
    使用 IntraWeb (15)
    使用 IntraWeb (14)
    使用 IntraWeb (13)
    使用 IntraWeb (12)
    使用 IntraWeb (11)
  • 原文地址:https://www.cnblogs.com/yanxiaoge/p/11828457.html
Copyright © 2011-2022 走看看