使用GridFsTemplate在Mongo中存取文件
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中存取文件的更多相关文章
- 使用GridFsTemplate在mongodb中存取文件
spring-data-mongodb之gridfs mongodb除了能够存储大量的数据外,还内置了一个非常好用的文件系统.基于mongodb集群的优势,GridFS当然也是分布式的,而且备份也 ...
- cpio - 存取归档包中的文件
总览 (SYNOPSIS) cpio {-o|--create} [-0acvABLV] [-C bytes] [-H format] [-M message] [-O [[user@]host:]a ...
- android中的文件操作详解以及内部存储和外部存储(转载)
原文链接:http://m.blog.csdn.net/article/details?id=17725989 摘要 其实安卓文件的操作和java在pc环境下的操作并无二致,之所以需要单独讲解是因为安 ...
- mongo中的模糊查询
以下是一个mongo查询的综合应用,即介绍一个生产中实际应用的模糊查询,当然其实也很简单,主要用到mongo中的模糊查询和$or查询,以及并的关系,下面是一个mongo中的一条记录 { "_ ...
- 【转】 android中的文件操作详解以及内部存储和外部存储
摘要 其实安卓文件的操作和Java在pc环境下的操作并无二致,之所以需要单独讲解是因为安卓系统提供了不同于pc的访问文件系统根路径的api,同时对一个应用的私有文件做了统一的管理.根据我的经验,初学者 ...
- 怎样在ado.net中存取excel和word呢?
办公软件指可以进行文字处理.表格制作.幻灯片制作.图形图像处理.简单数据库的处理等方面工作的软件.当然啦,这也包括了word.Excel以及PPT等.现在我们就一起来学习一下:怎样在ado.net中存 ...
- c#在sql中存取图片image示例
这篇文章主要介绍了c#在sql中存取图片image示例,需要的朋友可以参考下 (1)控制台应用程序下演示插入图片 复制代码 代码如下: public void InsertIMG() { //将需要存 ...
- find - 递归地在层次目录中处理文件
总览 SYNOPSIS find [path...] [expression] 描述 DESCRIPTION 这个文档是GNU版本 find 命令的使用手册. find 搜索目录树上的每一个文件名,它 ...
- 程序中的文件之沙盒以及plist文件的初步使用
沙盒是相对于"应用程序"的文件,也就是相相应app所在的页面的文件. 每个应用都有自己的应用沙盒(应用沙盒就是文件系统文件夹).与其它文件系统隔离.应用必须呆在在积极的沙盒中.其它 ...
随机推荐
- XML_Qt_资料
1.QXmlQuery Class _ Qt XML Patterns 5.7.html http://doc.qt.io/qt-5/qxmlquery.html ZC: evaluateTo(QAb ...
- GNU m4 教程[转]
原文:http://blog.csdn.net/timekeeperl/article/details/50738164 作者:garfileo 作者主页 本文整理自:https://segment ...
- hive row_number等窗口分析函数
一.排序&去重分析 row_number() over(partititon by col1 order by col2) as rn 结果:1,2,3,4 rank() over(parti ...
- 第106天:Ajax中同步请求和异步请求
同步请求和异步请求的区别 1.同步是指:发送方发出数据后,等接收方发回响应以后才发下一个数据包的通讯方式. 用户填写所有信息后,提交给服务器,等待服务器的回应(检验数据),是一次性的.信息错误又要重新 ...
- TitanX Server安装Caffe
服务器是Ubuntu Server 16.04,可以ssh和vnc连接. 安装caffe步骤 1. 安装anaconda2:这里不能用3,不知什么原因,cmake错误,无法生成pycaffe 2. 安 ...
- 331. Verify Preorder Serialization of a Binary Tree -- 判断是否为合法的先序序列
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, ...
- iOS笔记之文件读写
最近做的项目中要存储一组图片数据,图片带有name,date等属性,处理办法是讲image图片直接存在沙盒documents文件中,再使用plist文件存储图片属性和image路径. 存入图片: - ...
- git 基础学习笔记
配置ssh 打开命令行或者git bash 输入 创建git库 git init 检查当前状态 $ git status On branch master nothing to commit, wor ...
- SOA实践指南-读书笔记
SOA是英文Service-Oriented Architecture,即面向服务架构的缩写. SOA是一种范式,目的是增强灵活性.SOA很适宜处理复杂的分布式系统. SOA方法接受异质(不同的平台, ...
- Reusing & Composing GraphQL APIs with GraphQL Bindings
With GraphQL bindings you can embed existing GraphQL APIs into your GraphQL server. In previous blog ...