SpringBoot_文件存储_GridFS

た 入场券 2021-10-06 03:28 781阅读 0赞

SpringBoot_文件存储_GridFS

  • 依赖pom.xml
  • 配置文件bootstrap.properties
  • 配置源码

mongodb文件存储

依赖pom.xml

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-data-mongodb</artifactId>
  4. </dependency>

配置文件bootstrap.properties

  1. pingruan.base.gridfs-enable=true
  2. pingruan.base.gridfs-database=filecentor
  3. pingruan.base.gridfs-host=192.168.164.100
  4. pingruan.base.gridfs-port=27017
  5. pingruan.base.gridfs-uri=mongodb://root:qwe123@192.168.164.100:27017

配置源码

  1. //@Autowired
  2. //@Qualifier("gridFsUtil")
  3. //GridFsTemplate gridFsTemplate;
  4. /**
  5. * mongo文件存储-gridfs
  6. *
  7. * @author: vander
  8. * @create: 2018/12/05 11:19
  9. */
  10. @Configuration
  11. @ConditionalOnProperty(value="pingruan.base.gridfs-enable",havingValue="true")
  12. public class GridFsConfig {
  13. @Autowired
  14. BProperties bProperties;
  15. /**
  16. * 文件上传对象
  17. *
  18. * @return
  19. * @throws Exception
  20. */
  21. @Bean(name="gridFsUtil")
  22. public GridFsTemplate gridFsTemplate() {
  23. return new GridFsTemplate(mongoDb(),mappingMongoConverter2());
  24. }
  25. @Bean
  26. public GridFSBucket getGridFSBucket(){
  27. MongoClient mongoClient = new MongoClient(new MongoClientURI(bProperties.getGridfsUri()));
  28. MongoDatabase database = mongoClient.getDatabase(bProperties.getGridfsDatabase());
  29. GridFSBucket bucket = GridFSBuckets.create(database);
  30. return bucket;
  31. }
  32. @Bean
  33. public MongoMappingContext mongoMappingContext2() {
  34. MongoMappingContext mappingContext = new MongoMappingContext();
  35. return mappingContext;
  36. }
  37. @Bean
  38. public MappingMongoConverter mappingMongoConverter2() {
  39. DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(this.mongoDb());
  40. MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, this.mongoMappingContext2());
  41. converter.setTypeMapper(new DefaultMongoTypeMapper(null));
  42. return converter;
  43. }
  44. @Bean
  45. public MongoDbFactory mongoDb() {
  46. MongoClient mongoClient = new MongoClient(new MongoClientURI(bProperties.getGridfsUri()));
  47. return new SimpleMongoDbFactory(mongoClient,bProperties.getGridfsDatabase());
  48. }
  49. }
  50. /**
  51. * mongodb-gridfs文件系统
  52. *
  53. *
  54. * @author: vander
  55. * @create: 2018/12/05 17:43
  56. */
  57. @Controller
  58. @RequestMapping("gridfs")
  59. public class GridFSTestController extends BaseController {
  60. @Autowired(required=false)
  61. @Qualifier("gridFsUtil")
  62. GridFsTemplate gridFsTemplate;
  63. @Autowired(required=false)
  64. GridFSBucket gridFSBucket;
  65. @PostMapping("/upload")
  66. @ResponseBody
  67. public RestResult test(@RequestParam("file") MultipartFile file) throws IOException {
  68. String originalFilename = file.getOriginalFilename();
  69. InputStream inputStream = file.getInputStream();
  70. ObjectId yml = null;
  71. try {
  72. yml = gridFsTemplate.store(inputStream, originalFilename);
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. return ok(yml.toHexString());
  77. }
  78. @GetMapping("/download/{id}")
  79. public void testt(@PathVariable("id")String fileId) {
  80. //根据id查询文件
  81. GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
  82. //打开下载流对象
  83. GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
  84. //创建gridFsResource,用于获取流对象
  85. GridFsResource gridFsResource = new GridFsResource(gridFSFile, gridFSDownloadStream);
  86. //获取流中的数据
  87. InputStream inputStream = null;
  88. try {
  89. inputStream = gridFsResource.getInputStream();
  90. response.setCharacterEncoding(request.getCharacterEncoding());
  91. response.setContentType("application/octet-stream");
  92. response.setHeader("Content-Disposition", "attachment; filename=" + gridFsResource.getFilename());
  93. IOUtils.copy(inputStream, response.getOutputStream());
  94. response.flushBuffer();
  95. } catch (Exception e) {
  96. e.printStackTrace();
  97. } finally {
  98. try {
  99. inputStream.close();
  100. } catch (IOException e) {
  101. e.printStackTrace();
  102. }
  103. }
  104. }
  105. }

发表评论

表情:
评论列表 (有 0 条评论,781人围观)

还没有评论,来说两句吧...

相关阅读

    相关 GridFS持久化文件到MongoDB

    GridFS是MongoDB提供的用于持久化存储文件的模块。GridFS存储文件是将文件分块存储,文件会按照256KB的大小分割成多个块进行存储,GridFS使用两个集合(co

    相关 GridFS

    GridFS介绍 GridFS是MongoDB提供的用于持久化存储文件的模块,CMS使用MongoDB存储数据,使用GridFS可以快速集成开发。 //缺点是效率没有那么高