千家信息网

Spring Boot怎么使用GridFS实现文件的上传和下载方式

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,这篇文章主要讲解了"Spring Boot怎么使用GridFS实现文件的上传和下载方式",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Spring Bo
千家信息网最后更新 2026年11月01日Spring Boot怎么使用GridFS实现文件的上传和下载方式

这篇文章主要讲解了"Spring Boot怎么使用GridFS实现文件的上传和下载方式",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Spring Boot怎么使用GridFS实现文件的上传和下载方式"吧!

目录
  • 使用GridFS实现文件的上传和下载

    • 首先了解一下怎么用命令操作GridFS

    • 使用Spring Boot操作GridFS

  • Spring Boot中使用GridFS

    • 什么是GridFS

    • 在SpringBoot中使用GridFS

使用GridFS实现文件的上传和下载

在这篇博客中,我们将展示如何使用Spring Boot中使用mongodb自带的文件存储系统GridFS实现文件的上传和下载功能

首先了解一下怎么用命令操作GridFS

安装mongodb

sudo apt-get install mongodb

安装完成后找到mongofiles的位置

whereis mongofiles

找到之后进入目录就可以上传文件了

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

这时文件就添加成功了,进入mongodb查看

mongoshow dbs  # 发现多了一个gridfsuse gridfsdb.fs.files.find() # 存放文件的一些基本信息db.fs.chunks.find() # 存放文件二进制内容,一个文件有多个fs.chunks,每个fs.chunks都有一个,files_id与fs.files中的id相对应,形成多对一的关系,文件存的时候分开存,取的时候再合并

使用Spring Boot操作GridFS

引入依赖

 org.springframework.boot spring-boot-starter-data-mongodb commons-io    commons-io    2.4

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;@RestControllerpublic 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;@Configurationpublic 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

存储文件

@AutowiredGridFsTemplate gridFsTemplate;@Testpublic 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对象

@Configurationpublic 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;    }}
@AutowiredGridFsTemplate gridFsTemplate;@AutowiredGridFSBucket gridFSBucket;@Testpublic 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);}

删除文件

 //删除文件@Testpublic void testDelFile() throws IOException {//根据文件id删除fs.files和fs.chunks中的记录       gridFsTemplate.delete(Query.query(Criteria.where("_id").is("5c1b8fac72884e389ae3df82")));}

感谢各位的阅读,以上就是"Spring Boot怎么使用GridFS实现文件的上传和下载方式"的内容了,经过本文的学习后,相信大家对Spring Boot怎么使用GridFS实现文件的上传和下载方式这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

文件 存储 方式 信息 对象 内容 数据 学习 二进制 名称 命令 多个 大小 文件名 时候 时间 目录 查询 配置 各各 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网络科技 华服互联网科技靠谱吗 哈尔滨大商圈互联网科技有限公司 互联网科技有限公司图片 奇酷互联网络科技公司 北京禾田雨橡互联网科技 互联网科技美容 互联网 新科技 互联网科技股有哪些 华中科技有哪些互联网大佬 央行互联网金融科技公司 千方科技属于互联网企业吗 本届互联网博览会上的黑科技 中国科技创新与世界互联网大会 互联网科技圈年度人物 上海钢联属于互联网科技股吗 半导体行业属于互联网高科技类吗 九思连强互联网电子科技有限公司 华人运通互联网科技公司 青岛互联网科技园 咖啡网 科技赋能下的互联网金融 科技服务与互联网 碳银互联网科技 速维有品互联网信息科技(苏州)有限公司 长沙茂晟互联网科技有限公司 烟台灵鸽互联网科技有限公司 江苏互联网智能科技产品出厂价 现代互联网科技公司 长沙易易互联网科技有限公司
0