12
返回列表 发新帖
楼主: jieforest

Spring Data MongoDB支持GridFS

[复制链接]
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
11#
 楼主| 发表于 2012-8-3 01:02 | 只看该作者
Spring Data MongoDB offers a GridFsTemplate to handle the GridFS operations:

<!-- Connection to MongoDB server -->
<mongo:db-factory host="localhost" port="27017" dbname="test" />
<mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"/>
 
<!-- MongoDB GridFS Template -->
<bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
  <constructor-arg ref="mongoDbFactory"/>
  <constructor-arg ref="converter"/>
</bean>

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
12#
 楼主| 发表于 2012-8-3 01:03 | 只看该作者
With that template we can easily read the existing GridFS file we inserted before on the command line:
  1. @Autowired GridFsTemplate template;
  2.  
  3. @Test public void shouldListExistingFiles() {
  4.          List<GridFSDBFile> files = template.find(null);
  5.  
  6.          for (GridFSDBFile file: files) {
  7.                  System.out.println(file);
  8.          }
  9. }
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
13#
 楼主| 发表于 2012-8-3 01:03 | 只看该作者
本帖最后由 jieforest 于 2012-8-3 01:04 编辑

Spring Data MongoDB offers a GridFsTemplate to handle the GridFS operations:

  1. <!-- Connection to MongoDB server -->
  2. <mongo:db-factory host="localhost" port="27017" dbname="test" />
  3. <mongo:mapping-converter id="converter" db-factory-ref="mongoDbFactory"/>

  4. <!-- MongoDB GridFS Template -->
  5. <bean id="gridTemplate" class="org.springframework.data.mongodb.gridfs.GridFsTemplate">
  6.   <constructor-arg ref="mongoDbFactory"/>
  7.   <constructor-arg ref="converter"/>
  8. </bean>
复制代码

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
14#
 楼主| 发表于 2012-8-3 01:04 | 只看该作者
本帖最后由 jieforest 于 2012-8-3 01:04 编辑

With that template we can easily read the existing GridFS file we inserted before on the command line:
  1. @Autowired GridFsTemplate template;

  2. @Test public void shouldListExistingFiles() {
  3.          List<GridFSDBFile> files = template.find(null);

  4.          for (GridFSDBFile file: files) {
  5.                  System.out.println(file);
  6.          }
  7. }
复制代码


使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
15#
 楼主| 发表于 2012-8-3 01:05 | 只看该作者
Running the above example, you should see an output like this:
  1. { "_id" : { "$oid" : "4fe9bda0f2abbef0d127a647"} , "chunkSize" : 262144 , "length" : 2418176 ,
  2.    "md5" : "19c2a2cc7684ce9d497a59249396ae1d" , "filename" : "mongo.exe" , "contentType" :  null  ,
  3.   "uploadDate" : { "$date" : "2012-06-26T13:48:16.713Z"} , "aliases" :  null }
复制代码
In order to access the content of the file, you call GridFSDBFile#getInputStream. You can also store and delete GridFS files quite easily. Have a detailed look at the GridFsTemplate.
The full source code of the above example can be found at github.

使用道具 举报

回复
论坛徽章:
277
马上加薪
日期:2014-02-19 11:55:14马上有对象
日期:2014-02-19 11:55:14马上有钱
日期:2014-02-19 11:55:14马上有房
日期:2014-02-19 11:55:14马上有车
日期:2014-02-19 11:55:14马上有车
日期:2014-02-18 16:41:112014年新春福章
日期:2014-02-18 16:41:11版主9段
日期:2012-11-25 02:21:03ITPUB年度最佳版主
日期:2014-02-19 10:05:27现任管理团队成员
日期:2011-05-07 01:45:08
16#
 楼主| 发表于 2012-8-3 01:05 | 只看该作者
本帖最后由 jieforest 于 2012-8-3 01:06 编辑

Running the above example, you should see an output like this:
  1. { "_id" : { "$oid" : "4fe9bda0f2abbef0d127a647"} , "chunkSize" : 262144 , "length" : 2418176 ,
  2.    "md5" : "19c2a2cc7684ce9d497a59249396ae1d" , "filename" : "mongo.exe" , "contentType" :  null  ,
  3.   "uploadDate" : { "$date" : "2012-06-26T13:48:16.713Z"} , "aliases" :  null }
复制代码


In order to access the content of the file, you call GridFSDBFile#getInputStream. You can also store and delete GridFS files quite easily. Have a detailed look at the GridFsTemplate.

The full source code of the above example can be found at github.





使用道具 举报

回复

您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

TOP技术积分榜 社区积分榜 徽章 团队 统计 知识索引树 积分竞拍 文本模式 帮助
  ITPUB首页 | ITPUB论坛 | 数据库技术 | 企业信息化 | 开发技术 | 微软技术 | 软件工程与项目管理 | IBM技术园地 | 行业纵向讨论 | IT招聘 | IT文档
  ChinaUnix | ChinaUnix博客 | ChinaUnix论坛
CopyRight 1999-2011 itpub.net All Right Reserved. 北京盛拓优讯信息技术有限公司版权所有 联系我们 未成年人举报专区 
京ICP备16024965号-8  北京市公安局海淀分局网监中心备案编号:11010802021510 广播电视节目制作经营许可证:编号(京)字第1149号
  
快速回复 返回顶部 返回列表