使用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所在的页面的文件. 每个应用都有自己的应用沙盒(应用沙盒就是文件系统文件夹).与其它文件系统隔离.应用必须呆在在积极的沙盒中.其它 ...
随机推荐
- 一次由webview报错引起的追根溯源
最近客户端那边需要搞了个h5嵌入app,想想是移动端的webview,前端这边也比较忙.就没想太多,直接用了async/await处理api数据,于是就不怪测试就来搞事情了... 一.error: 1 ...
- Jenkins+Sonar集成对代码进行持续检测
介绍 SonarQube(曾用名Sonar(声纳)[1])是一个开源的代码质量管理系统. 特征 支持超过25种编程语言[2]:Java.C/C++.C#.PHP.Flex.Groovy.JavaScr ...
- [转载]CentOS 6.3安装Subversion服务器
转载自http://www.cnblogs.com/zhoulf/archive/2013/02/02/2889949.html 安装说明 系统环境:CentOS-6.3 安装方式:yum insta ...
- CentOS6.5系统服务
服务名称 功能 默认 建议 备注说明 NetworkManager 用于自动连接网络,常用在Laptop上 开启 关闭 对服务器无用 abrt-ccpp 开启 自定 对服务器无用 abrt-oop ...
- 201621123010《Java程序设计》第7周学习总结
1. 本周学习总结 1.1思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. addActionListener(new Actio ...
- pshell远程连接服务器
在页面添加ip 和 端口 还有 用户,我这里填的是服务器root用户 成功之后 端口后是可以改的 首先看下ssh是否启动 rpm -qa | grep ssh 有的话就是vi /etc/ ...
- Coding(码市)教程(一):基础配置
作者:Adaaaagio 出处:http://www.cnblogs.com/zyx418 欢迎转载,希望能够帮到更多的人,转载也请保留这段申明,谢谢! 初识coding是在新入职的公司,前辈说我们用 ...
- LG3781 [SDOI2017]切树游戏
题意 题目描述 小Q是一个热爱学习的人,他经常去维基百科学习计算机科学. 就在刚才,小Q认真地学习了一系列位运算符,其中按位异或的运算符\(\oplus\)对他影响很大.按位异或的运算符是双目运算符. ...
- vue路由初始化路转
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- java中读取配置文件
若是Javaweb项目,项目运行于tomcat或其他容器时,可以使用下面方式来获取文件的输入流 1.当属性文件放在src下面时 InputStream is = Thread.currentThrea ...