【轉發】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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM