基于Mongodb的文件上传,下载,预览和更新
创建maven工程并导入相关依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sixa.demo</groupId>
<artifactId>mongo-fileupload-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
</project>
全局配置文件
spring:
data:
mongodb:
authentication-database: admin
host: 127.0.0.1
port: 27017
username: sixa
password: root
database: sixa
实体类
业务实体类FileEntity
package com.sixa.demo.entity;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author liuan@dist.com.cn
* @data 2021/8/6
*/
@Document(collection = "fileEntity")
public class FileEntity {
private long id;
private String fileId;
private String filename;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getFileId() {
return fileId;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public String getFilename() {
return filename;
}
public void setFilename(String filename) {
this.filename = filename;
}
}
结果展示类FileResult
package com.sixa.demo.entity;
/**
* @author liuan@dist.com.cn
* @data 2021/8/4
*/
public class FileResult {
private String id;
private String fileName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
}
增长序列实体类Sequence
package com.sixa.demo.entity;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.Document;
/**
* @author liuan@dist.com.cn
* @data 2021/8/6
*/
@Document
public class Sequence {
@Indexed(unique = true)
private String tableName;
private long seq;
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public long getSeq() {
return seq;
}
public void setSeq(long seq) {
this.seq = seq;
}
}
Staff类
package com.sixa.demo.entity;
import org.springframework.data.mongodb.core.mapping.Document;
import java.util.Date;
/**
* @author liuan@dist.com.cn
* @data 2021/8/4
*/
@Document
public class Staff {
private String id;
private String name;
private int age;
private Date entryDate;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Date getEntryDate() {
return entryDate;
}
public void setEntryDate(Date entryDate) {
this.entryDate = entryDate;
}
}
Dao层
package com.sixa.demo.mapper;
import com.sixa.demo.entity.FileEntity;
import org.springframework.data.mongodb.repository.MongoRepository;
/**
* @author liuan@dist.com.cn
* @data 2021/8/6
*/
public interface FileEntityDao extends MongoRepository<FileEntity,Long> {
FileEntity findByFilename(String name);
}
package com.sixa.demo.mapper;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.sixa.</