- 准备工作
- 在pom.xml文件中引入相应依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>com.mongodb</groupId>
<artifactId>mongodb-driver</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.mongodb</groupId>
<artifactId>mongodb-driver-core</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.4.3</version>
</dependency>
- 创建MongoDB配置类,并在application.properties文件中配置MongoDB相关的配置项:
MongoDB配置类:
@Configuration
public class MongoDBConfig {
@Bean
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory){
return new MongoTemplate(mongoDbFactory);
}
}
application.properties文件中的配置项:
# MongoDB配置
spring.data.mongodb.uri=mongodb://127.0.0.1:27017/test
- 使用GridFs存储文件
- 在controller类中注入mongoTemplate,并使用mongoTemplate操作GridFs。
GridFs操作分为两步:先将文件存储到GridFs数据库,再将文件的ObjectId存储到普通MongoDB数据库中(这里是book数据库的book_image集合)。下面是一个示例:
@RestController
public class GridFsController {
@Autowired
private MongoTemplate mongoTemplate;
@RequestMapping(value = "/gridfs", method = RequestMethod.POST)
public String uploadGridFs(@RequestParam("file") MultipartFile file) throws IOException{
DB db = mongoTemplate.getDb();
GridFS gridFS = new GridFS(db, "book_image");// 获取GridFS对象
GridFSInputFile gridFSInputFile = gridFS.createFile(file.getInputStream());// 创建GridFS文件
gridFSInputFile.setFilename(file.getOriginalFilename());// 设置文件名称
gridFSInputFile.setContentType(file.getContentType());// 设置文件类型
gridFSInputFile.save();// 保存文件到GridFS库
String fileId = gridFSInputFile.getId().toString();// 获取文件ID
DBObject query = new BasicDBObject("bookId", "123456");// 将文件ID存储到book_image集合中(以bookId为条件)
DBObject update = new BasicDBObject("$set", new BasicDBObject("imageId", fileId));
mongoTemplate.getCollection("book_image").update(query, update, true, false);
return "success";
}
}
- 使用GridFs查询文件
- 同样,在controller类中使用mongoTemplate查询数据。根据普通MongoDB集合book_image中的ObjectId,从GridFs中查询文件并返回。
@RestController
public class GridFsController {
@Autowired
private MongoTemplate mongoTemplate;
@RequestMapping(value = "/gridfs", method = RequestMethod.GET)
public ResponseEntity<byte[]> getGridFsImage() throws IOException{
String bookId = "123456";// 可根据业务需要修改查询条件
BasicDBObject query = new BasicDBObject();
query.put("bookId", bookId);
String imageId = "";
DBCursor cursor = mongoTemplate.getCollection("book_image").find(query);
while(cursor.hasNext()){
BasicDBObject obj = (BasicDBObject) cursor.next();
imageId = obj.getString("imageId");
}
GridFS gridFS = new GridFS(mongoTemplate.getDb(), "book_image");
GridFSDBFile gridFSDBFile = gridFS.findOne(imageId);
byte[] bytes = IOUtils.toByteArray(gridFSDBFile.getInputStream());
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.IMAGE_PNG);
return new ResponseEntity<byte[]>(bytes, headers, HttpStatus.OK);
}
}
以上就是使用SpringBoot和MongoDB的GridFs操作分布式文件存储的完整攻略,包括文件存储和文件查询两方面的实现示例。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:SpringBoot 使用Mongo的GridFs实现分布式文件存储操作 - Python技术站