Spring Boot使用GridFS实现文件的上传和下载
在这篇博客中,我们将展示如何使用Spring Boot中使用mongodb自带的文件存储系统GridFS实现文件的上传和下载功能
首先了解一下怎么用命令操作GridFS
安装mongodb
sudo apt-get install mongodb
安装完成后找到mongofiles的位置
whereis mongofiles
找到之后进入目录就可以上传文件了
mongofiles put /home/ubuntu/Desktop/1.jpg
这时文件就添加成功了,进入mongodb查看
mongo
show dbs # 发现多了一个gridfs
use gridfs
db.fs.files.find() # 存放文件的一些基本信息
db.fs.chunks.find() # 存放文件二进制内容,一个文件有多个fs.chunks,每个fs.chunks都有一个,files_id与fs.files中的id相对应,形成多对一的关系,文件存的时候分开存,取的时候再合并
使用Spring Boot操作GridFS
引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
controller代码
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.GridFSFindIterable;
import com.mongodb.client.gridfs.model.GridFSFile;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereFilename;
@RestController
public class ImageController {
@Autowired
private GridFsTemplate gridFsTemplate;
@Autowired
private GridFsOperations operations;
@Autowired
private GridFSBucket gridFSBucket;
@PostMapping("file/upload")
public ImageResponse upload(@RequestParam("file") MultipartFile file) {
DBObject metaData = new BasicDBObject();
// 把时间戳作为文件名存入mongodb
String fileName = String.valueOf(System.currentTimeMillis());
InputStream inputStream = null;
try {
inputStream = file.getInputStream();
gridFsTemplate.store(inputStream, fileName, "image", metaData);
} catch (IOException e) {
throw new RuntimeException();
}
return new ImageResponse(fileName);
}
@GetMapping(value = "file/download/${fileName}", produces = MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public byte[] getImage(@PathVariable("fileName") String fileName) throws IOException {
if (fileName == null) {
return null;
}
// 根据文件名查询(也可以根据md5值等信息查询)
GridFSFindIterable result = operations.find(query(whereFilename().is(fileName)));
GridFSFile gridFSFile = result.first();
GridFSDownloadStream gridFSDownloadStream = gridFSBucket.openDownloadStream(gridFSFile.getObjectId());
//创建gridFsResource,用于获取流对象
GridFsResource gridFsResource = new GridFsResource(gridFSFile, gridFSDownloadStream);
return IOUtils.toByteArray(gridFsResource.getInputStream());
}
}
加入一个配置类
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;
@Configuration
public class MongoConfig {
@Value("${spring.data.mongodb.database}")
String db;
@Bean
public GridFSBucket getGridFSBucket(MongoClient mongoClient){
MongoDatabase database = mongoClient.getDatabase(db);
GridFSBucket bucket = GridFSBuckets.create(database);
return bucket;
}
}
图片返回的实体类
public class ImageResponse implements Serializable {
private String imageName;
public ImageResponse(String imageName) {
this.imageName = imageName;
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
}
配置文件
spring:
data:
mongodb:
uri: mongodb://localhost:27017/gridfs
database: gridfs