SpringBoot_文件存储_GridFS
mongodb文件存储
依赖pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
配置文件bootstrap.properties
pingruan.base.gridfs-enable=true
pingruan.base.gridfs-database=filecentor
pingruan.base.gridfs-host=192.168.164.100
pingruan.base.gridfs-port=27017
pingruan.base.gridfs-uri=mongodb://root:[email protected]:27017
配置源码
//@Autowired
//@Qualifier("gridFsUtil")
//GridFsTemplate gridFsTemplate;
/**
* mongo文件存储-gridfs
*
* @author: vander
* @create: 2018/12/05 11:19
*/
@Configuration
@ConditionalOnProperty(value="pingruan.base.gridfs-enable",havingValue="true")
public class GridFsConfig {
@Autowired
BProperties bProperties;
/**
* 文件上传对象
*
* @return
* @throws Exception
*/
@Bean(name="gridFsUtil")
public GridFsTemplate gridFsTemplate() {
return new GridFsTemplate(mongoDb(),mappingMongoConverter2());
}
@Bean
public GridFSBucket getGridFSBucket(){
MongoClient mongoClient = new MongoClient(new MongoClientURI(bProperties.getGridfsUri()));
MongoDatabase database = mongoClient.getDatabase(bProperties.getGridfsDatabase());
GridFSBucket bucket = GridFSBuckets.create(database);
return bucket;
}
@Bean
public MongoMappingContext mongoMappingContext2() {
MongoMappingContext mappingContext = new MongoMappingContext();
return mappingContext;
}
@Bean
public MappingMongoConverter mappingMongoConverter2() {
DefaultDbRefResolver dbRefResolver = new DefaultDbRefResolver(this.mongoDb());
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, this.mongoMappingContext2());
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return converter;
}
@Bean
public MongoDbFactory mongoDb() {
MongoClient mongoClient = new MongoClient(new MongoClientURI(bProperties.getGridfsUri()));
return new SimpleMongoDbFactory(mongoClient,bProperties.getGridfsDatabase());
}
}
/**
* mongodb-gridfs文件系统
*
*
* @author: vander
* @create: 2018/12/05 17:43
*/
@Controller
@RequestMapping("gridfs")
public class GridFSTestController extends BaseController {
@Autowired(required=false)
@Qualifier("gridFsUtil")
GridFsTemplate gridFsTemplate;
@Autowired(required=false)
GridFSBucket gridFSBucket;
@PostMapping("/upload")
@ResponseBody
public RestResult test(@RequestParam("file") MultipartFile file) throws IOException {
String originalFilename = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
ObjectId yml = null;
try {
yml = gridFsTemplate.store(inputStream, originalFilename);
} catch (Exception e) {
e.printStackTrace();
}
return ok(yml.toHexString());
}
@GetMapping("/download/{id}")
public void testt(@PathVariable("id")String fileId) {
//根据id查询文件
GridFSFile gridFSFile = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(fileId)));
//打开下载流对象
GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
//创建gridFsResource,用于获取流对象
GridFsResource gridFsResource = new GridFsResource(gridFSFile, gridFSDownloadStream);
//获取流中的数据
InputStream inputStream = null;
try {
inputStream = gridFsResource.getInputStream();
response.setCharacterEncoding(request.getCharacterEncoding());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=" + gridFsResource.getFilename());
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}