zoukankan      html  css  js  c++  java
  • 阶段5 3.微服务项目【学成在线】_day04 页面静态化_18-页面静态化-模板管理-GridFS研究-取文件

    需要创建mongoDB的配置类1


    配置类里面主要创建。GridFSBucket这个对象。这个对象的作用就是用来打开一个下载流

    在cms的微服务下,在config下创建MongoConfig。这个时候就需要用到spring的注解。@Configuration。加上这个注解。这个类就相当于是一个Bean。
    用这个标识的类,spring的容器子在启动的时候。会扫描到这个Bean,然后就会把这个Bean注册到IOC容器中


    这个类就是从配置文件中读取到mongo的database。

    在创建GridFSBucket的时候需要指定是哪个数据库

    package com.xuecheng.manage_cms.config;
    
    import com.mongodb.MongoClient;
    import com.mongodb.client.MongoDatabase;
    import com.mongodb.client.gridfs.GridFSBucket;
    import com.mongodb.client.gridfs.GridFSBuckets;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.data.mongodb.gridfs.GridFsTemplate;
    
    @Configuration
    public class MongoConfig {
        @Value("${spring.data.mongodb.database}")
        String db;
    
        @Bean
        public GridFSBucket getGridFsTemplate(MongoClient mongoClient){
            MongoDatabase database = mongoClient.getDatabase(db);
            GridFSBucket bucket = GridFSBuckets.create(database);
            return bucket;
        }
    }


    这是配置文件内配置的mongo的数据库信息

    测试类测试

    cms的微服务下,测试类GridFsTest内。注入GridFsBucket。目的是打开一个下载流

    查询用到Criteria,Criteria就是一个条件对象。

    criteria

    美 [kraɪ'tɪriən]
    英 [kraɪ'tɪəriən]
    • n.标准;尺度
    • 网络准则;条件;规准






    把content复制出来。

    最终代码

    package com.xuecheng.manage_cms;
    
    import com.mongodb.client.gridfs.GridFSBucket;
    import com.mongodb.client.gridfs.GridFSDownloadStream;
    import com.mongodb.client.gridfs.model.GridFSFile;
    import org.apache.commons.io.IOUtils;
    import org.bson.types.ObjectId;
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    import org.springframework.data.mongodb.core.query.Criteria;
    import org.springframework.data.mongodb.core.query.Query;
    import org.springframework.data.mongodb.gridfs.GridFsResource;
    import org.springframework.data.mongodb.gridfs.GridFsTemplate;
    import org.springframework.test.context.junit4.SpringRunner;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class GridFsTest {
        @Autowired
        GridFsTemplate gridFsTemplate;
        @Autowired
        GridFSBucket gridFSBucket;
        @Test
        public void testGridFsTemplate() throws FileNotFoundException {
            File file = new File("d:/index_banner.ftl");
            FileInputStream fileInputStream = new FileInputStream(file);
    
            //定义fileInputSream
            ObjectId objectId = gridFsTemplate.store(fileInputStream, "index_banner.ftl");
            System.out.println(objectId);
        }
        @Test
        public void queryFile() throws IOException {
            //根据文件id查询文件
            GridFSFile gridFsFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is("5dbeb89bface36388cb8c7d4")));
            //打开一个下载流对象
            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);
        }
    }

    删除文件

    自行测试

     

  • 相关阅读:
    【Lua】LuaForWindows_v5.1.4-46安装失败解决方案
    【C++】指针引发的bug
    【C++】指针引发的bug
    【C++】位操作(3)-获取某位的值
    bzoj1444
    bzoj1758
    bzoj3091
    poj1741 bzoj2152
    bzoj2125 3047
    bzoj3669
  • 原文地址:https://www.cnblogs.com/wangjunwei/p/11594947.html
Copyright © 2011-2022 走看看