springBoot学习之mongoDB gridfs文件操作

gridfs文件操作

1.依赖和系统配置文件

pom

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.4.RELEASE</version>
    <relativePath/>
</parent>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

application.properties

#mongodb
spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=gridfs
#集群
#spring.data.mongodb.uri=mongodb://user:pwd@ip1:port1,ip2:port2/database
#spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test

这里用的springboot版本是2.0.4版本,使用GridFsTemplate的findOne方法,返回的GGridFSDBFile更好为GGridFSFile,文件下载时不能使用以前的GridFSDBFile 操作流了。

GridFSDBFile gridfs = fs.findOne(new BasicDBObject("_id", id));
gridfs.writeTo(file);

需要把GGridFSFile转换为GGridFsResource,主要需要获取GGridFSDownloadStream(由GridFSBucket获取)

2.代码

  • 配置类
@Configuration
public class MongoConf {
    @Autowired
    private MongoDbFactory mongoDbFactory;
    @Autowired
    private GridFSBucket gridFSBucket;


    @Bean
    public GridFSBucket getGridFSBuckets() {
        MongoDatabase db = mongoDbFactory.getDb();
        return GridFSBuckets.create(db);
    }
}
  • 测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class MongodbTest {
    @Autowired
    private GridFsTemplate gridFsTemplate;

    @Autowired
    private GridFsOperations operations;

    @Autowired
    private GridFSBucket gridFSBucket;

    @Test
    public void delete() throws  Exception{
        gridFsTemplate.delete(query(where("_id").is("5b8a44ba63f89e2bc47285ad")));
    }

    @Test
    public void getFile() throws Exception{
        GridFSFile file = gridFsTemplate.findOne(query(whereFilename().is("a.png")));
        if(file != null){
            System.out.println("_id:"+file.getId());
            System.out.println("_objectId:"+file.getObjectId());
            GridFSDownloadStream in = gridFSBucket.openDownloadStream(file.getObjectId());

            GridFsResource resource = new GridFsResource(file,in);
            InputStream inputStream = resource.getInputStream();
            byte[] f = getBytes(inputStream);


            FileOutputStream out = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\lg.png");
            out.write(f);
        }

    }

    private byte[] getBytes(InputStream inputStream) throws  Exception{
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int  i = 0;
        while (-1!=(i=inputStream.read(b))){
            bos.write(b,0,i);
        }
        return bos.toByteArray();
    }

    @Test
    public void storeFile() throws  Exception{
        org.springframework.core.io.Resource resource = new FileSystemResource("C:\\Users\\Administrator\\Desktop\\a.png");
        ObjectId id = gridFsTemplate.store(resource.getInputStream(), resource.getFilename(), ".png");
        System.out.println("_id:"+id);
    }
}


  • 结果

调用文件上传store()
这里写图片描述

还有文件下载和删除就不一一赘述了

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值