原文出自: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: 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, ...
深入学习MongoDB:Scaling MongoDB && 50 Tips and Tricks for MongoDB Developers深入学习MongoDB中文版Scaling MongoDB英文版50 Tips and Tricks for MongoDB Developers英文版高清完整目录3本打包合集
**Spring Data MongoDB: 更新文档** 在现代Web应用开发中,数据存储是至关重要的部分,而MongoDB作为NoSQL数据库中的代表,因其灵活性和高性能而受到广泛欢迎。Spring Data MongoDB是Spring框架的一个模块,它简化...
1. 连接MongoDB:输入`mongo.exe`命令,连接到本地MongoDB服务。 2. 创建数据库:使用`use <database_name>`命令,如`use testdb`,创建一个名为"testdb"的数据库。 3. 插入数据:在选定的数据库中,使用`db....
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); MongoDatabase database = mongoClient.getDatabase("testDB"); // ...其他数据库操作 } } ``` 以上代码展示了如何创建一个到...
"java+MongoDB实现存图片、下载图片的方法示例" 本文主要介绍了使用java和MongoDB实现存图片和下载图片的方法,并结合实例形式详细分析了java结合MongoDB实现图片的存储和下载相关操作技巧。 Java和MongoDB简介 ...
MongoClient mongoClient = MongoClients.create("mongodb://root:root@localhost:27017"); // 获取名为"mydb"的数据库 MongoDatabase database = mongoClient.getDatabase("mydb"); ``` 现在,你可以对数据库进行...
MongoClient mongoClient = MongoClients.create("mongodb://username:password@localhost:27017"); ``` 然后,我们从`MongoClient`获取到`MongoDatabase`和`MongoCollection`对象,这代表了我们的数据库和集合。...
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); ``` 4. 使用基本操作: - 连接数据库:`MongoDatabase db = mongoClient.getDatabase("databaseName");` - 操作集合:`...
MongoClient mongoClient = MongoClients.create("mongodb://192.168.46.195:10001"); MongoDatabase database = mongoClient.getDatabase("testDB"); MongoCollection<Document> collection = database....
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); ``` 五、数据库与集合操作 1. 获取数据库:使用`MongoDatabase`对象表示数据库,例如`mydb`。 ```java MongoDatabase database = ...
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); ``` 然后,选择或创建数据库: ```java MongoDatabase database = mongoClient.getDatabase("myDatabase"); ``` 接着是集合操作。...
- MongoDB:以键值对、文档、集合的形式存储数据,灵活的数据结构,适合半结构化和非结构化数据。 2. 查询语言: - MySQL:使用SQL(结构化查询语言)进行查询,支持JOIN操作,适合处理结构化关系数据。 - ...
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); MongoDatabase database = mongoClient.getDatabase("myDatabase"); System.out.println("Connected to MongoDB!"); mongoClient...
本文将深入探讨如何使用Java的JDBC(Java Database Connectivity)接口与MongoDB、MySQL数据库进行数据交互,并实现相互之间的数据同步。 首先,我们需要理解JDBC,它是Java语言访问数据库的标准API,能够连接并...
将SQL表转换为MongoDB集合的Java工具 轻松将 MSSQL 表转换为 MongoDB。 从下载 SQL Server JDBC 驱动程序 将 SQLJDBC4.jar 文件放在同一文件夹中。 一次指定 TABLE NAME 和 NUMBER OF ROWS 进行导入。 java -...
MongoClient mongoClient = MongoClients.create("mongodb://localhost:27017"); MongoDatabase database = mongoClient.getDatabase("testDatabase"); System.out.println("Connected to MongoDB on port 27017...
implementation 'org.mongodb:mongodb-driver-core:3.4.2' implementation 'org.mongodb:bson:3.4.2' } ``` 一旦添加了依赖,你就可以开始编写Java代码来连接MongoDB数据库。以下是一个基本的示例: ```java ...