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

最新文章

  1. 我的MYSQL学习心得(三) 查看字段长度
  2. 6-3 bash脚本编程之五 字符串测试及for循环
  3. Android万能使用WebServices(不用引入外部包)
  4. 关于HttpHandler的相关知识总结
  5. nginx 反向代理 google
  6. Openstack的dashboard开发之【浏览器兼容性】
  7. mysql笔记05 优化服务器设置
  8. ios 可变参数(va_list,va_start,va_end)
  9. 柔性数组-读《深度探索C++对象模型》有感 (转载)
  10. Javascript 思维导图
  11. CDialogSK - A Skinnable Dialog Class
  12. Windows最常用的几个网络CMD命令总结
  13. ViewFilpper
  14. Macaca 自动化框架 [Python 系列]
  15. PHP实现excel
  16. Alpha冲刺Day6
  17. idea设置调用方法时提示方法注释
  18. linux 新建用户、用户组 以及为新用户分配权限的基本操作
  19. hdu5505-GT and numbers-(贪心+gcd+唯一分解定理)
  20. 004_加速国内docker源下载速度

热门文章

  1. bzoj2463: [中山市选2009]谁能赢呢? 博弈
  2. HDU - 4804 Campus Design 轮廓线dp
  3. Codeforces Round #418 (Div. 2)D
  4. Intel IDEA 2018破解(亲测成功)
  5. vue router动态路由
  6. git 基础入门操作
  7. json.dumps与json.dump的区别 json.loads与json.load的区别(简洁易懂)
  8. 在Jsp中调用静态资源,路径配置问题,jsp获取路径的一些方法
  9. specialized English for automation-Lesson 3 CMOS Logic Circuit
  10. CH3401 石头游戏