【转发】springBoot学习之mongoDB gridfs文件操作

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qw12312312/article/details/82288438
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=127.0.0.1 //地址
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()


还有文件下载和删除就不一一赘述了
————————————————
版权声明:本文为CSDN博主「qw12312312」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qw12312312/article/details/82288438

posted @ 2019-11-01 15:36  涂小二  阅读(1473)  评论(0编辑  收藏  举报