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

使用GridFsTemplate在Mongo中存取文件的更多相关文章

  1. 使用GridFsTemplate在mongodb中存取文件

    spring-data-mongodb之gridfs   mongodb除了能够存储大量的数据外,还内置了一个非常好用的文件系统.基于mongodb集群的优势,GridFS当然也是分布式的,而且备份也 ...

  2. cpio - 存取归档包中的文件

    总览 (SYNOPSIS) cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] [-O [[user@]host:]a ...

  3. android中的文件操作详解以及内部存储和外部存储(转载)

    原文链接:http://m.blog.csdn.net/article/details?id=17725989 摘要 其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安 ...

  4. mongo中的模糊查询

    以下是一个mongo查询的综合应用,即介绍一个生产中实际应用的模糊查询,当然其实也很简单,主要用到mongo中的模糊查询和$or查询,以及并的关系,下面是一个mongo中的一条记录 { "_ ...

  5. 【转】 android中的文件操作详解以及内部存储和外部存储

    摘要 其实安卓文件的操作和Java在pc环境下的操作并无二致,之所以需要单独讲解是因为安卓系统提供了不同于pc的访问文件系统根路径的api,同时对一个应用的私有文件做了统一的管理.根据我的经验,初学者 ...

  6. 怎样在ado.net中存取excel和word呢?

    办公软件指可以进行文字处理.表格制作.幻灯片制作.图形图像处理.简单数据库的处理等方面工作的软件.当然啦,这也包括了word.Excel以及PPT等.现在我们就一起来学习一下:怎样在ado.net中存 ...

  7. c#在sql中存取图片image示例

    这篇文章主要介绍了c#在sql中存取图片image示例,需要的朋友可以参考下 (1)控制台应用程序下演示插入图片 复制代码 代码如下: public void InsertIMG() { //将需要存 ...

  8. find - 递归地在层次目录中处理文件

    总览 SYNOPSIS find [path...] [expression] 描述 DESCRIPTION 这个文档是GNU版本 find 命令的使用手册. find 搜索目录树上的每一个文件名,它 ...

  9. 程序中的文件之沙盒以及plist文件的初步使用

    沙盒是相对于"应用程序"的文件,也就是相相应app所在的页面的文件. 每个应用都有自己的应用沙盒(应用沙盒就是文件系统文件夹).与其它文件系统隔离.应用必须呆在在积极的沙盒中.其它 ...

随机推荐

  1. PHP+MySql+jQuery实现的“顶”和“踩”投票功能

    index.html <!DOCTYPE HTML><html><head><meta charset="utf-8"><ti ...

  2. 五句话搞定JavaScript作用域【转】

    JavaScript的作用域一直以来是前端开发中比较难以理解的知识点,对于JavaScript的作用域主要记住几句话,走遍天下都不怕... 一.“JavaScript中无块级作用域” 在Java或C# ...

  3. 【源码学习之spark core 1.6.1 各种部署模式所使用的的TaskSceduler及SchedulerBackend】

    说明:个人原创,转载请说明出处 http://www.cnblogs.com/piaolingzxh/p/5656879.html 未完待续 未完待续

  4. MySQL查询in操作排序

    in操作排序 先说解决方案: select * from test where id in(3,1,5) order by field(id,3,1,5); 或许有人会注意过,但我以前真不知道 SQL ...

  5. Spring MVC 实现跨域资源 CORS 请求

    说到 AJAX 跨域,很多人最先想到的是 JSONP.的确,JSONP 我们已经十分熟悉,也使用了多年,从本质上讲,JSONP 的原理是给页面注入一个 <script>,把远程 JavaS ...

  6. 99. Recover Binary Search Tree -- 找到二叉排序树中交换过位置的两个节点

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. IOS-常用第三方开源框架介绍

    iOS开发-常用第三方开源框架介绍(你了解的ios只是冰山一角) 时间:2015-05-06 16:43:34      阅读:533      评论:0      收藏:0      [点我收藏+] ...

  8. web漏洞扫描工具集合

    最好用的开源Web漏洞扫描工具梳理 链接:www.freebuf.com/articles/web/155209.html 赛门铁克2017年互联网安全威胁报告中提出在他们今年扫描的网站中,有76%都 ...

  9. ActiveX开发

    转自(http://blog.csdn.net/mingojiang/article/details/8159263) 一.ActiveX基础 1.1什么是ActiveX ActiveX是COM规范的 ...

  10. CSS用法

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...