当前位置:网站首页>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;
边栏推荐
猜你喜欢
Message queue design based on mysql
【目标检测】YOLOv7理论简介+实践测试
Mysql基础篇(Mysql数据类型)
Simulation of Active anti-islanding-AFD Active Anti-islanding Model Based on Simulink
MySQL4
Pyspark机器学习:向量及其常用操作
Article summary: the basic model of VPN and business types
typescript25 - type assertion
Input输入框光标在前输入后自动跳到最后面的bug
出现Command ‘vim‘ is available in the following places,vim: command not found等解决方法
随机推荐
2. # code comments
Mysql中的数据类型和运算符
Invalid classes inferred from unique values of `y`. Expected: [0 1 2], got [1 2 3]
开源许可证 GPL、BSD、MIT、Mozilla、Apache和LGPL的区别
Visual Studio提供的 Command Prompt 到底有啥用
MySQL3
使用ts-node报错
动态规划 01背包
数组问题之《下一个排列》、《旋转图像》以及二分查找之《搜索二维矩阵》
【目标检测】YOLOv7理论简介+实践测试
Flutter Tutorial 02 Introduction to Flutter Desktop Program Development Tutorial Run hello world (tutorial includes source code)
这里有110+公开的专业数据集
The 16th day of the special assault version of the sword offer
leetcode6132. Make all elements in an array equal to zero (simple, weekly)
软件测试周刊(第82期):其实所有纠结做选择的人心里早就有了答案,咨询只是想得到内心所倾向的选择。
解决ffmpeg使用screen-capture-recorder录屏,有屏幕缩放的情况下录不全的问题
Software Testing Interview (3)
How to promote new products online?
What is dynamic programming and what is the knapsack problem
typescript25-类型断言