本文来说下如何使用GridFsTemplate来实现文件归类
程序测试
mave文件
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
yml文件
spring:
application:
name: spirng-boot-mongodb
data:
mongodb:
host: localhost #同127.0.0.1
port: 27017
database: test #指定操作的数据库
配置文件
@Configuration
public class MongoDBConfig {
@Value("${spring.data.mongodb.database}")
private String db;
@Bean(name = "gridFsPdfBucket")
public GridFSBucket getGridFsPdfBucket(MongoClient mongoClient){
MongoDatabase mongoDatabase = mongoClient.getDatabase(db);
// 注意,这里的bucketName与需要下载的存储桶要保持一致,否则会到默认的fs存储桶中进行下载
GridFSBucket bucket = GridFSBuckets.create(mongoDatabase,"pdf_gfs");
return bucket;
}
/***
* 自定义桶的名称
* @param dbFactory
* @param converter
* @return
*/
@Bean(name = "gridFsPdfTemplate")
public GridFsTemplate gridFsTestTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter) {
return new GridFsTemplate(dbFactory, converter, "pdf_gfs");
}
//可以把不同类型的文件上传到不同的存储桶中进行进行管理
@Bean(name = "gridFsPictureBucket")
public GridFSBucket getGridFsPictureBucket(MongoClient mongoClient){
MongoDatabase mongoDatabase = mongoClient.getDatabase(db);
// 注意,这里的bucketName与需要下载的存储桶要保持一致,否则会到默认的fs存储桶中进行下载
GridFSBucket bucket = GridFSBuckets.create(mongoDatabase,"picture_gfs");
return bucket;
}
@Bean(name = "gridFsPictureTemplate")
public GridFsTemplate gridFsPictureTemplate(MongoDatabaseFactory dbFactory, MongoConverter converter) {
return new GridFsTemplate(dbFactory, converter, "picture_gfs");
}
}
测试程序
@Slf4j
@RestController
@RequestMapping("/api/file/mongodb")
@Api(tags = "文件上传至mongodb数据库开发测试")
public class FileCommonController {
// 文件都上传到pdf_gfs这个存储桶中
@Resource(name = "gridFsPdfTemplate")
private GridFsTemplate gridFsTemplate;
// 从pdf_gfs这个存储桶中下载文件
@Resource(name = "gridFsPdfBucket")
private GridFSBucket gridFSBucket;
/**
* 通用上传单个
* 文件请求
*/
@SneakyThrows
@PostMapping("/uploadSingleFile")
@ApiOperation(value = "上传单个文件")
public ResponseBean<?> uploadSingleFile(MultipartFile file) {
// 新文件名
String originalFilename = file.getOriginalFilename();
// 获得文件输入流
InputStream ins = file.getInputStream();
// 获得文件类型
String contentType = file.getContentType();
ObjectId objectId = gridFsTemplate.store(ins, originalFilename, contentType);
log.info(objectId.toString());
log.info("接收文件:" + originalFilename);
//返回信息
return new ResponseBean<>(200, "上传成功", objectId.toString());
}
@ApiOperation(value = "使用MongoDB的ID来下载文件")
@GetMapping(value = "/downloadFileById")
@SneakyThrows
public byte[] downloadFileById(@RequestParam(name = "file_id") String fileId, HttpServletResponse response) {
Query query = Query.query(Criteria.where("_id").is(fileId));
GridFSFile gridFSFile = gridFsTemplate.findOne(query);
log.info("文件信息如下 =============>" + gridFSFile);
// 解决文件下载的时候,中文乱码的问题
String name = new String(gridFSFile.getFilename().getBytes("GBK"), "ISO-8859-1");
response.setHeader("Content-Disposition", "attachment;fileName=" + name);
GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
//创建gridFsResource,用于获取流对象
GridFsResource gridFsResource = new GridFsResource(gridFSFile, gridFSDownloadStream);
log.info(gridFSFile.getFilename() + "=======>下载成功!!");
return IOUtils.toByteArray(gridFsResource.getInputStream());
}
}
测试结果
本文小结
本文编写了一个测试程序使用GridFsTemplate实现文件归类,当然都保存到默认的fs存储桶中也是可以的。