使用GridFsTemplate在mongodb中存取文件
来源:互联网 发布:mysql update 性能 编辑:程序博客网 时间:2024/05/16 11:25
Maven依赖(还有一些springboot需要的)
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent> <properties> <java.version>1.8</java.version> </properties> <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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> <repositories> <repository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release</url> </repository> </repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> <name>Spring Releases</name> <url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories>
配置GridFsTemplate
import com.mongodb.Mongo;import com.mongodb.MongoClient;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.core.env.Environment;import org.springframework.data.mongodb.MongoDbFactory;import org.springframework.data.mongodb.config.AbstractMongoConfiguration;import org.springframework.data.mongodb.core.SimpleMongoDbFactory;import org.springframework.data.mongodb.gridfs.GridFsTemplate;import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;@Configuration@EnableMongoRepositories// 读取配置文件的,通过Environment读取@PropertySource("classpath:mongo.yml")public class GridFsConfiguration extends AbstractMongoConfiguration { @Autowired Environment env; @Bean public GridFsTemplate gridFsTemplate() throws Exception { return new GridFsTemplate(mongoDbFactory(), mappingMongoConverter()); } @Override protected String getDatabaseName() { return env.getProperty("database"); } @Override public Mongo mongo() throws Exception { return new MongoClient(env.getProperty("host")); } @Bean public MongoDbFactory mongoDbFactory() throws Exception { return new SimpleMongoDbFactory(new MongoClient(env.getProperty("host"), env.getProperty("port", Integer.class)), getDatabaseName()); }}
mongo.yml
mongo: host: 127.0.0.1 port: 27017 database: filecenter
使用GridFsTemplate存取文件
import com.mongodb.gridfs.GridFSDBFile;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.core.io.ClassPathResource;import org.springframework.core.io.Resource;import org.springframework.data.mongodb.gridfs.GridFsResource;import org.springframework.data.mongodb.gridfs.GridFsTemplate;import org.springframework.test.context.junit4.SpringRunner;import java.io.File;import java.io.IOException;import java.util.List;import static org.springframework.data.mongodb.core.query.Query.query;import static org.springframework.data.mongodb.gridfs.GridFsCriteria.whereFilename;@RunWith(SpringRunner.class)@SpringBootTest(classes = GridFsConfiguration.class)public class MongoTest { @Autowired GridFsTemplate gridFsTemplate; // 存文件 @Test public void storeFileInGridFs() { Resource file = new ClassPathResource("mongo.yml"); // Resource file = new FileSystemResource("C:\\Users\\Chenggaowei\\Downloads\\Adblock.crx"); try { gridFsTemplate.store(file.getInputStream(), file.getFilename(), "yml"); } catch (IOException e) { e.printStackTrace(); } } // 下载文件 @Test public void findFilesInGridFs() throws IOException { List<GridFSDBFile> result = gridFsTemplate.find(query(whereFilename().is("mongo.yml"))); GridFSDBFile gridFSDBFile = result.get(0); gridFSDBFile.writeTo(new File("C:\\Users\\Chenggaowei\\Downloads\\" + gridFSDBFile.getFilename())); } // 所有文件 @Test public void readFilesFromGridFs() { GridFsResource[] txtFiles = gridFsTemplate.getResources("*"); for (GridFsResource txtFile : txtFiles) { System.out.println(txtFile.getFilename()); } }}
参考文献
https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#gridfs
阅读全文
0 0
- 使用GridFsTemplate在mongodb中存取文件
- c# 使用base64在数据库中存取文件
- MongoDB文件存取操作
- MongoDB文件存取操作
- MongoDB文件存取
- MongoDB文件存取操作
- 在Sql server中存取word文件!
- VB在Oracle数据库中存取文件
- 使用C#在數據庫中存取文件
- MongoDB GridFS java存取文件
- mongodb存取图片文件功能
- MongoDB GridFS java存取文件
- 使用pycassa 在cassandra中存取数据
- MongoDB MongoDB文件存取操作(4)
- Mongodb学习笔记三、使用asp.net在Mongodb中存储和读取图片文件
- 数据库中存取文件
- 数据库中存取文件
- 数据库中存取文件
- UVA11624 Fire! —— BFS
- jdbc查询的工具类
- 从Vue.js源码角度再看数据绑定
- oracle_为何在查询中索引未被使用
- 死锁/活锁/锁饥饿
- 使用GridFsTemplate在mongodb中存取文件
- 巨星天龙
- c++基础之指针
- Codeforces 474E Pillars dp+线段树
- Cookie与Session的区别
- ZooKeeper_12_会话-重连
- 离京前记--带宝贝爬长城小感想
- let definitions are not supported by current javascript
- 周01