Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

The principle and Application of GridFS

Shulou Source: shulou.com Published: 2022-06-02 08:51:08 10月12日 Update

This article introduces the relevant knowledge of "the principle and application of GridFS". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

GridFS is a module provided by MongoDB for persistent file storage. CMS uses MongoDB to store data. FGridFS can be used for rapid integration development.

How it works:

Storage of files in GridFS is to store files in blocks. Files are divided into multiple blocks for storage according to the size of 256KB. GridFS uses two collections to store files. One collection is chunks, which is used to store binary data of files; the other collection is files, which is used to store metadata of files (file name, size, upload time, etc.).

Getting Started Code:

1. Add dependencies

org.springframework.boot spring-boot-starter-data-mongodb org.springframework.boot spring-boot-starter-test org.springframework.boot spring-boot-starter-web org.apache.commons commons-io 1.3.2

II. Creating a Startup Class

package lianbang.wu.gridfs;import com.mongodb.MongoClient;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Bean;@SpringBootApplicationpublic class GridFSApplication { public static void main(String[] args) { SpringApplication.run(GridFSApplication.class); }}

III. Configuration files

server: port: 31001spring: application: name: xc-service-manage-cms data: mongodb: uri: mongodb://root:123@localhost:27017 #mongodb connection database: xc-cms #database name

IV. Creating Configuration Classes

@Configurationpublic class GridFsConfig { @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; }}

V. Test code

public class GridFSTest { @Autowired GridFsTemplate gridFsTemplate; @Autowired GridFSBucket gridFSBucket; //save file @Test public void testGridFs() throws FileNotFoundException { File file = new File("d:/index_banner.html"); FileInputStream fileInputStream = new FileInputStream(file); ObjectId objectId = gridFsTemplate.store (fileInputStream, "Test File"); String fileId = objectId.toString(); System.out.println(fileId); } //read file @Test public void queryFile() throws IOException { String fileId = "123456"; GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId))); GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId()); GridFsResource gridFsResource = new GridFsResource(gridFSFile,gridFSDownloadStream); String string = IOUtils.toString(gridFsResource.getInputStream(), "utf-8"); System.out.println(string); } //Delete files @Test public void testDelFile(){ gridFsTemplate.delete(Query.query(Criteria.where("_id").is("1234"))); }} The content of "Principles and Applications of GridFS" is introduced here. Thank you for reading it. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

Tags: File storage data principle application code content name size more knowledge testing configuration utility learning next two binary information dilemma Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Huawei Microsoft Redmi NVidia Xiaomi