IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

MongoDB-GridFS基本操作总结

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

54_1.png

##

前言

最近spring boot项目中用到mongodb的gridFs,把mongodb当文件系统来用配置操作有些多,这里对基本操作做个总结,以后忘记了可以即时来查阅。[]~( ̄▽ ̄)~*

使用步骤

1. pom配置

spring boot中集成了mongodb,直接引入就行

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

2. yml配置

就是一个通用的mongoDB数据库配置

spring:  
    application:    
        name: [应用服务名]
    data:    
        mongodb:      
            uri:  mongodb://localhost:27017 [mongdb的url]     
            database: xc_cms [数据库名]

3. config类配置

基本包结构和启动类配置好了以后,建一个 GridFsTemplateConfig类

54_2.png

@Configuration
public class GridFsTemplateConfig {

    @Value("${spring.data.mongodb.database}")
    private String db;

    @Bean
    public GridFSBucket getGridFSBucket(MongoClient mongoClient) {
        MongoDatabase database = mongoClient.getDatabase(db);
        return GridFSBuckets.create(database);
    }
}

4. 新建测试

建立一个测试类54_3.png

先贴一下基本框架代码

@SpringBootTest
@RunWith(SpringRunner.class)
public class GridFsTemplateTest {

    @Resource   // 这个是spring容器里原来就有的,直接调用
    private GridFsTemplate gridFsTemplate;

    @Resource  // 这个是之前在config里配置的
    private GridFSBucket gridFSBucket;


}

5. 保存文件到GridFS

    @Test
    public void testGridFs() throws FileNotFoundException {
        File file = new File("D:\\2.png");
        FileInputStream fileInputStream = new FileInputStream(file);
//    参数 content=fileInputStream  filename="测试用例2"  contentType="", 返回fileID
        ObjectId objectId = gridFsTemplate.store(fileInputStream, "测试用例2", "");
        String fileId = objectId.toString();
        System.out.println(fileId);
    }

看一下gridFsTemplate.store方法

        /*
     * (non-Javadoc)
     * @see org.springframework.data.mongodb.gridfs.GridFsOperations#store(java.io.InputStream, java.lang.String, java.lang.String)
     */
    public ObjectId store(InputStream content, @Nullable String filename, @Nullable String contentType) {
        return store(content, filename, contentType, (Object) null);
    }

开启测试,窗口中打印出了fileID

54_4.png

查看数据库xc_cms-> fs.files集合,最后一个document就是插入的文件信息,文件里面的具体数据保存在了fs.chunks集合

54_5.png

6. 从GridFs中读文件

就用上次的保存的文件为例(fileId: 5e858277c0e732084022cfcf),下面是基本模板

    @Test
    public void testGridBucket() throws IOException {
        String fileId = "5e858277c0e732084022cfcf";
//      根据id查询文件
        GridFSFile fsFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
//      打开下载流对象
        GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(fsFile.getObjectId());
//        创建gridFsResource,用于获取流对象
        GridFsResource gridFsResource = new GridFsResource(fsFile, gridFSDownloadStream);
        String s = IOUtils.toString(gridFsResource.getInputStream(), StandardCharsets.UTF_8);
        System.out.println(s);
    }

因为测试的是一个png文件,所以打印出来的是….~( ̄▽ ̄)~*

54_6.png

7. 从GridFs中删除文件

    @Test
    public void testDeleteFile() {
        gridFsTemplate.delete(Query.query(Criteria.where("_id").is("5e858277c0e732084022cfcf")));
    }

成功54_7.png

以上是个人总结,也希望对你有帮助 φ(゜▽゜*)♪

文章永久链接:https://tech.souyunku.com/?p=29476


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(93) 打赏



未经允许不得转载:搜云库技术团队 » MongoDB-GridFS基本操作总结

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367