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. java
譯:在本教程中,咱們將向你展現如何經過 GridFS API 保存一個圖片到MongoDB。GridFS APIs 提供將其餘二進制文件的支持,好比視頻和音頻等。 mongodb
Note
For detail explanation, read this MongoDB GridFS manual. 數據庫
譯:詳細解釋,請閱讀MongoDB GridFS 文檔. app
1. Save image
Code snippets to save an image file into MongoDB, under 「photo」 namespace, and assign a new 「filename」 for the saved image. ide
譯:下面代碼片段爲保存一個圖片文件到MongoDB,在"photo"命名空間下,將圖片文件取一個新名保存。 this
Java代碼
- 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」. spa
譯:下面代碼片段,根據文件名,獲取保存的圖片。 .net
Java代碼
- 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. orm
Json代碼
- {
- "_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中獲取全部保存的文件,並用數據庫遊標迭代輸出。
Java代碼
- 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中獲取一個圖片文件並輸出(生成另外一個圖片)。
Java代碼
- 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.
譯:下面代碼片段,刪除一個圖片文件。
Java代碼
- 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"圖片文件。
Java代碼
- 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 GridFS Specification