Spring Boot使用GridFS實現文件的上傳和下載方式

使用GridFS實現文件的上傳和下載

在這篇博客中,我們將展示如何使用Spring Boot中使用mongodb自帶的文件存儲系統GridFS實現文件的上傳和下載功能

首先瞭解一下怎麼用命令操作GridFS

安裝mongodb

sudo apt-get install mongodb

安裝完成後找到mongofiles的位置

whereis mongofiles

找到之後進入目錄就可以上傳文件瞭

mongofiles put /home/ubuntu/Desktop/1.jpg

這時文件就添加成功瞭,進入mongodb查看

mongo
show dbs  # 發現多瞭一個gridfs
use gridfs
db.fs.files.find() # 存放文件的一些基本信息
db.fs.chunks.find() # 存放文件二進制內容,一個文件有多個fs.chunks,每個fs.chunks都有一個,files_id與fs.files中的id相對應,形成多對一的關系,文件存的時候分開存,取的時候再合並

使用Spring Boot操作GridFS

引入依賴

<dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
 <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
</dependency>

controller代碼

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.GridFSFindIterable;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereFilename;
@RestController
public class ImageController {
    @Autowired
    private GridFsTemplate gridFsTemplate;
    @Autowired
    private GridFsOperations operations;
    @Autowired
    private GridFSBucket gridFSBucket;
    @PostMapping("file/upload")
    public ImageResponse upload(@RequestParam("file") MultipartFile file) {
        DBObject metaData = new BasicDBObject();
        // 把時間戳作為文件名存入mongodb
        String fileName = String.valueOf(System.currentTimeMillis());
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
            gridFsTemplate.store(inputStream, fileName, "image", metaData);
        } catch (IOException e) {
            throw new RuntimeException();
        }
        return new ImageResponse(fileName);
    }
    @GetMapping(value = "file/download/${fileName}", produces = MediaType.IMAGE_JPEG_VALUE)
    @ResponseBody
    public byte[] getImage(@PathVariable("fileName") String fileName) throws IOException {
        if (fileName == null) {
            return null;
        }
        // 根據文件名查詢(也可以根據md5值等信息查詢)
        GridFSFindIterable result = operations.find(query(whereFilename().is(fileName)));
        GridFSFile gridFSFile = result.first();
        GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
        //創建gridFsResource,用於獲取流對象
        GridFsResource gridFsResource = new GridFsResource(gridFSFile, gridFSDownloadStream);
        return IOUtils.toByteArray(gridFsResource.getInputStream());
    }
}

加入一個配置類

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;
@Configuration
public class MongoConfig {
    @Value("${spring.data.mongodb.database}")
    String db;
    @Bean
    public GridFSBucket getGridFSBucket(MongoClient mongoClient){
        MongoDatabase database = mongoClient.getDatabase(db);
        GridFSBucket bucket = GridFSBuckets.create(database);
        return bucket;
    }
}

圖片返回的實體類

public class ImageResponse implements Serializable {
    private String imageName;
    public ImageResponse(String imageName) {
        this.imageName = imageName;
    }
    public String getImageName() {
        return imageName;
    }
    public void setImageName(String imageName) {
        this.imageName = imageName;
    }
}

配置文件

spring:
  data:
    mongodb:
      uri: mongodb://localhost:27017/gridfs
      database: gridfs

Spring Boot中使用GridFS

什麼是GridFS

GirdFS是MongoDB提供的用於持久化存儲文件的模塊

在GridFS存儲文件是將文件分塊存儲,文件會按照256KB的大小分割成多個塊進行存儲,GridFS使用兩個集合 (collection)存儲文件,一個集合是chunks, 用於存儲文件的二進制數據;一個集合是files,用於存儲文件的元數 據信息(文件名稱、塊大小、上傳時間等信息)。

從GridFS中讀取文件要對文件的各各塊進行組裝、合並。

在SpringBoot中使用GridFS

存儲文件

@Autowired
GridFsTemplate gridFsTemplate;
@Test
public void GridFsTest() throws FileNotFoundException {
   //選擇要存儲的文件
   File file = new File("/Users/xxx/Desktop/xxx.docx");
   InputStream inputStream = new FileInputStream(file);
   //存儲文件並起名稱
   ObjectId objectId = gridFsTemplate.store(inputStream, "面試寶典");
   String id = objectId.toString();
   //獲取到文件的id,可以從數據庫中查找
   System.out.println(id);
}

查找文件

創建GridFSBucket對象

@Configuration
public class MongoConfig {
    @Value("${spring.data.mongodb.database}")
    String db;
    @Bean
    public GridFSBucket getGridFSBucket(MongoClient mongoClient){
        MongoDatabase mongoDatabase = mongoClient.getDatabase(db);
        GridFSBucket bucket = GridFSBuckets.create(mongoDatabase);
        return bucket;
    }
}
@Autowired
GridFsTemplate gridFsTemplate;
@Autowired
GridFSBucket gridFSBucket;
@Test
public void queryFile() throws IOException {
   String id = "5c1b8fac72884e389ae3df82";
   //根據id查找文件
   GridFSFile gridFSFile = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
   //打開下載流對象
   GridFSDownloadStream gridFS = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
   //創建gridFsSource,用於獲取流對象
   GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFS);
   //獲取流中的數據
   String string = IOUtils.toString(gridFsResource.getInputStream(), "UTF-8");
   System.out.println(string);
}

刪除文件

 //刪除文件
@Test
public void testDelFile() throws IOException {
//根據文件id刪除fs.files和fs.chunks中的記錄
       gridFsTemplate.delete(Query.query(Criteria.where("_id").is("5c1b8fac72884e389ae3df82")));
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: