原文出自:http://www.mkyong.com/mongodb/java-mongodb-save-image-example/
返回目录:http://ysj5125094.iteye.com/blog/2192754
Java MongoDB : Save image example
In this tutorial, we show you how to save an image file into MongoDB, via GridFS API. The GridFS APIs are able to serve other binary files as well, like video and music files.
译:在本教程中,我们将向你展示如何通过 GridFS API 保存一个图片到MongoDB。GridFS APIs 提供将其他二进制文件的支持,比如视频和音频等。
Note
For detail explanation, read this MongoDB GridFS manual.
译:详细解释,请阅读MongoDB GridFS 文档.
1. Save image
Code snippets to save an image file into MongoDB, under “photo” namespace, and assign a new “filename” for the saved image.
译:下面代码片断为保存一个图片文件到MongoDB,在"photo"命名空间下,将图片文件取一个新名保存。
String newFileName = "mkyong-java-image"; File imageFile = new File("c:\\JavaWebHosting.png"); GridFS gfsPhoto = new GridFS(db, "photo"); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(newFileName); gfsFile.save();
2. Get image
Code snippets to get the saved image by its “filename”.
译:下面代码片断,根据文件名,获取保存的图片。
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); System.out.println(imageForOutput);
Output, the image is saved as following JSON format.
{ "_id" : { "$oid" : "4dc9511a14a7d017fee35746" } , "chunkSize" : 262144 , "length" : 22672 , "md5" : "1462a6cfa27669af1d8d21c2d7dd1f8b" , "filename" : "mkyong-java-image" , "contentType" : null , "uploadDate" : { "$date" : "2011-05-10T14:52:10Z" } , "aliases" : null }
3. Print all saved images
Code snippets to get all the saved files from MongoDB and iterate it with DBCursor.
译:下面代码片断,从MongoDB中获取所有保存的文件,并用数据库游标迭代输出。
GridFS gfsPhoto = new GridFS(db, "photo"); DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); }
4. Save into another image
Code snippets to get an image file from MongoDB and output it to another image file.
译:下面代码片断,从MongoDB中获取一个图片文件并输出(生成另一个图片)。
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); //output to new file
5. Delete image
Code snippets to delete an image file.
译:下面代码片断,删除一个图片文件。
String newFileName = "mkyong-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); gfsPhoto.remove(gfsPhoto.findOne(newFileName));
Full Example
Full example to work with image, via Java MongoDB GridFS API. See comments for explanation.
注:运行程序之前,一定要在C盘创建“c:\\JavaWebHosting.png"图片文件。
package com.mkyong.core; import java.io.File; import java.io.IOException; import java.net.UnknownHostException; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.DBCursor; import com.mongodb.Mongo; import com.mongodb.MongoException; import com.mongodb.gridfs.GridFS; import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSInputFile; /** * Java MongoDB : Save image example * */ public class SaveImageApp { public static void main(String[] args) { try { Mongo mongo = new Mongo("localhost", 27017); DB db = mongo.getDB("imagedb"); DBCollection collection = db.getCollection("dummyColl"); String newFileName = "mkyong-java-image"; File imageFile = new File("c:\\JavaWebHosting.png"); // create a "photo" namespace GridFS gfsPhoto = new GridFS(db, "photo"); // get image file from local drive GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); // set a new filename for identify purpose gfsFile.setFilename(newFileName); // save the image file into mongoDB gfsFile.save(); // print the result DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); } // get image file by it's filename GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); // save it into a new image file imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); // remove the image file from mongoDB gfsPhoto.remove(gfsPhoto.findOne(newFileName)); System.out.println("Done"); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
At the end of the program, a new image file is created in “c:\\JavaWebHostingNew.png“.
译:执行程序之后,会创建一个新的图片文件“c:\\JavaWebHostingNew.png“。
Reference
相关推荐
MongoDB权威指南:MongoDB:The Definitive Guide第一版 第二版 中、英文4本合集
《Windows上安装MongoDB:完整步骤详解.pdf》是一份适用于Windows操作系统的MongoDB安装教程,适用于初学者和有经验的开发人员。本教程包括完整的步骤详解,可帮助您快速掌握MongoDB的安装配置,包括环境变量设置,...
深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers深入学习MongoDB中文版Scaling MongoDB英文版50 Tips and Tricks for MongoDB Developers英文版高清完整目录3本打包合集
MongoDB: The Definitive Guide MongoDB is a powerful, flexible, and scalable generalpurpose database. It combines the ability to scale out with features such as secondary indexes, range queries, ...
将SQL表转换为MongoDB集合的Java工具 轻松将 MSSQL 表转换为 MongoDB。 从下载 SQL Server JDBC 驱动程序 将 SQLJDBC4.jar 文件放在同一文件夹中。 一次指定 TABLE NAME 和 NUMBER OF ROWS 进行导入。 java -...
使用java向mongodb中插入数据 一、Linux安装mongodb 二、MongoDB客户端工具(MongoVUE)访问mongodb 三、通过Java操作MongoDB
Written by engineers from 10gen, the company that develops and supports this open source database, MongoDB: The Definitive Guide provides guidance for database developers, advanced configuration for ...
java mongodb orm 实现-基于mongodb-driver开发的ORM
描述 PM2模块可自动监视mongodb的生命体征: 查询,输入,更新,删除 连接数 已用存储空间 网络速度(输入和输出) 代表名称和状态 pm2-mongodb ... pm2 set pm2-mongodb:ip 42.42.42.42 (我的mongod
羽毛mongodb 用于数据库适配器,使用用于。 $ npm install --save mongodb feathers-mongodb ... connect ( 'mongodb://localhost:27017/feathers' ) . then ( client => { app . use ( '/messages' ,
docker run -d --name mongodb -p 27017:27017 -v /data/mongodb:/var/lib/mongodb anapsix/mongodb 作为客户: docker run -it --rm anapsix/mongodb mongo --help 配置 您可以像通常那样通过命令行传递配置选项...
安装$ npm install mqemitter-mongodb --save例子var mongodb = require ( 'mqemitter-mongodb' )var mq = mongodb ( { url : 'mongodb://127.0.0.1/mqemitter?auto_reconnect'} )var msg = { topic : 'hello world'...
mongoDB:MongoDB备忘单
mongodb人偶模块 目录 概述 从OS存储库或从MongoDB社区/企业存储库在RHEL / Ubuntu / Debian上安装MongoDB。 模块说明 ...如果只想使用默认选项安装服务器,则可以运行include mongodb::server 。
Spring集成MongoDB官方指定jar包:spring-data-mongodb-1.4.1.RELEASE.jar
1.java连接mongo数据库查询统计信息导出为excel表格 2.所有连接参数都可以动态输入,参数包括:ip、端口、数据库名称、集合名称、查询参数(公众号,写在指定文件中),导出表格存放位置
MongoDB Java操作大全 源代码 实例
Stitch SDK弃用通知Stitch SDK功能已移至Realm SDK。 使用之一,可以将使用... 安卓将以下内容添加到您的应用程序模块的build.gradle中: implementation ' org.mongodb:stitch-android-sdk:4.7.0 ' 还将以下内容添加到
java 操作mongodb代码,可直接运行成功,作为初学者非常实用。其中数据库是包含用户名和密码,用户名root ,密码root。
主要介绍了java+MongoDB实现存图片、下载图片的方法,结合实例形式详细分析了java结合MongoDB实现图片的存储与下载相关操作技巧,需要的朋友可以参考下