当前位置:网站首页>56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
56:第五章:开发admin管理服务:9:开发【文件上传到,MongoDB的GridFS中,接口】;(把文件上传到GridFS的SOP)
2022-08-01 04:28:00 【小枯林】
说明:
(1)本篇博客内容:开发【文件上传到,MongoDB的GridFS中,接口】;
目录
三:在【files】文件服务的application.yml配置文件中,配置MongoDB;
四:在【files】文件服务中,创建一个配置类GridFSConfig类;(该类的主要作用是:实例化一个MongoDB的GridFSBucket对象,进IoC容器)
2.在【files】文件服务的FileUploaderController类中,去实现【文件上传到,MongoDB的GridFS中】接口;
一:GridFS Buckets,简介;
(1)在【45:第四章:开发文件服务:6:第三方云存储解决方案【阿里云OSS】;】中,介绍阿里OSS的时候,也有BUcket的概念;
(2)其实,很多文件存储系统,文件都是放在Bucket中的;
(3)我们这儿,也需要在项目中整合GridFS的Buckets;
二:在【model】模型工程中,引入MongoDB依赖;
<!-- 引入 mongodb 依赖 --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver</artifactId> </dependency>
三:在【files】文件服务的application.yml配置文件中,配置MongoDB;
#配置MongoDB data: mongodb: uri: mongodb://root:[email protected]***.***:27017 #MongoDB所部署服务的ip地址、端口号; database: imooc-news #我们需要在MongoDB中,创建imooc-news这个database;说明:
(1) 内容说明;
四:在【files】文件服务中,创建一个配置类GridFSConfig类;(该类的主要作用是:实例化一个MongoDB的GridFSBucket对象,进IoC容器)
package com.imooc.files; 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; import org.springframework.stereotype.Component; @Configuration public class GridFSConfig { @Value("${spring.data.mongodb.database}") private String mongodb; /** * 向IoC容器中,实例化一个GridFSBucket对象; * @param mongoClient * @return */ @Bean public GridFSBucket gridFSBucket(MongoClient mongoClient) { //通过MongoClient,获得MongoDatabase;参数是,MongoDB中的某个database; MongoDatabase mongoDatabase = mongoClient.getDatabase(mongodb); //通过获得MongoDatabase,获得GridFSBucket; GridFSBucket gridFSBucket = GridFSBuckets.create(mongoDatabase); //返回获得的GridFSBucket;也就是把这个GridFSBucket实例化进IoC容器; return gridFSBucket; } }说明:
(1)利用@Value从application.yml这个官方配置文件中,获取定义的属性值;(有关这个点,可以参考【28:第三章:开发通行证服务:11:在配置文件中定义属性,然后在代码中去获取;】)
(2)这样一来,我们在其他地方就可以直接从IoC容器中,获取GridFSBucket对象对象了;
五:正式开发;
1.在【api】接口工程的FileUploaderControllerApi接口中,定义【文件上传到,MongoDB的GridFS中】接口;(具体在这儿,这个接口的作用就是【把人脸数据,上传到MongoDB的GridFS中】,也就是人脸入库)
/** * 【文件上传到MongoDB的GridFS】 * (1)和【AdminMngControllerApi中的,新增admin账号,接口】一样;我们还是使用NewAdminBO来承接 * 前端传过来的参数;然后,具体的人脸数据会存在NewAdminBO的img64属性中;(PS:后端之所以可以这么干,前端肯定做了对应的设置的) * (2)这个接口,我们是不能够通过Swagger进行调用的?所以,Swagger的@ApiOperation()我们就不设置了; * (3)其实,不仅人脸数据可以上传到GridFS,一些其他文件,只要需要我们也可以上传到GridFS; * @param newAdminBO * @return * @throws Exception */ @PostMapping("/uploadToGridFS") //设置路由,这个是需要前后端约定好的; public GraceJSONResult uploadToGridFS(@RequestBody NewAdminBO newAdminBO) throws Exception;说明:
(1)这个接口的url、请求方式、参数类型,不是瞎写的,是需要和前端保持一致的;
(2)在向GridFS上传文件时,我们会使用在【51:第五章:开发admin管理服务:4:开发【新增admin账号,接口】;】中,创建的NewAdminBO实体类,来承接数据;
(3)合理性说明:
(4)能够看到,前端做了很多工作;
2.在【files】文件服务的FileUploaderController类中,去实现【文件上传到,MongoDB的GridFS中】接口;
/** * 【文件上传到,MongoDB的GridFS中】 * @param newAdminBO * @return * @throws Exception */ @Override public GraceJSONResult uploadToGridFS(NewAdminBO newAdminBO) throws Exception { // 1.1 获得img64属性值,这个值其实就是文件(人脸图片文件)的Base64编码后的字符串; String file64 = newAdminBO.getImg64(); // 1.2 把base64格式的字符串,转换成byte数组; byte[] bytes = new BASE64Decoder().decodeBuffer(file64.trim()); // 1.3 把byte数组,转成InputStream输入流; ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); /** * 2. 把文件上传到MongoDB的GridFS中; * 第一个参数:上传后的文件名;我们这儿使用【管理员用户名.png】的名字;因为管理员用户名不能重复,所以这种命名方式是OK的; * 第二个参数:文件的InputStream输入流; * 返回值:其返回值类型是ObjectId(org.bson.types); */ ObjectId fileId = gridFSBucket.uploadFromStream(newAdminBO.getAdminName() + ".png", byteArrayInputStream); // 3. 获得文件在GridFS中的主键id; String fileIdStr = fileId.toString(); // 4. 把文件上传到GridFS中后的id,回传给前端; return GraceJSONResult.ok(fileIdStr); }说明:
(1) 看注释吧;
六:测试;
(1)install一下整个项目;(2)然后,启动【admin】管理服务和【files】文件服务;(3)记得启动前端,使用SwitchHost开启虚拟域名;
然后,其逻辑为:
(1)step1;
(2)step2;
边栏推荐
- 产品经理访谈 | 第五代验证码的创新与背景
- UE4 制作遇到的问题
- [FPGA tutorial case 43] Image case 3 - image sobel edge extraction through verilog, auxiliary verification through MATLAB
- Visual Studio提供的 Command Prompt 到底有啥用
- PMP 相关方管理必背总结
- UE4 rays flashed from mouse position detection
- 6-23漏洞利用-postgresql代码执行利用
- 这里有110+公开的专业数据集
- 简单易用的任务队列-beanstalkd
- MySQL3
猜你喜欢

这里有110+公开的专业数据集

typescript27 - what about enumeration types

typescript19-对象可选参数

【愚公系列】2022年07月 Go教学课程 023-Go容器之列表

High Numbers | 【Re-integration】Line Area Score 880 Examples

数组问题之《下一个排列》、《旋转图像》以及二分查找之《搜索二维矩阵》

6-23漏洞利用-postgresql代码执行利用

IJCAI2022 | Hybrid Probabilistic Reasoning with Algebraic and Logical Constraints

MLP neural network, GRNN neural network, SVM neural network and deep learning neural network compare and identify human health and non-health data

MySQL4
随机推荐
PMP 项目资源管理
typescript23-元组
JS new fun(); class and instance JS is based on object language Can only act as a class by writing constructors
Write a method to flatten an array and deduplicate and sort it incrementally
黑客到底可以厉害到什么程度?
【愚公系列】2022年07月 Go教学课程 025-递归函数
Typescript22 - interface inheritance
UE4 rays flashed from mouse position detection
Immutable
怀念故乡的月亮
UE4 模型OnClick事件不生效的两种原因
「以云为核,无感极速」顶象第五代验证码
PMP 项目沟通管理
让你的 Lottie 支持文字区域内自动换行
The Flow Of Percona Toolkit pt-table-checksum
Mysql基础篇(Mysql数据类型)
typescript25-类型断言
Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
TypeScript simplifies running ts-node
认真对待每一个时刻










