您的位置:首页 > 博客中心 > 数据库 >

Java MongoDB : Save image example

时间:2022-03-14 09:34

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 .

译:详细解释,请阅读文档.

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"命名空间下,将图片文件取一个新名保存。

Java代码  
  1. String newFileName = "mkyong-java-image";  
  2. File imageFile = new File("c:\\JavaWebHosting.png");  
  3. GridFS gfsPhoto = new GridFS(db, "photo");  
  4. GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);  
  5. gfsFile.setFilename(newFileName);  
  6. gfsFile.save();  

2. Get image

Code snippets to get the saved image by its “filename”.

译:下面代码片断,根据文件名,获取保存的图片。

Java代码  
  1. String newFileName = "mkyong-java-image";  
  2. GridFS gfsPhoto = new GridFS(db, "photo");  
  3. GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);  
  4. System.out.println(imageForOutput);  

 

Output, the image is saved as following JSON format.

Json代码  
  1. {   
  2.     "_id" :   
  3.     {   
  4.         "$oid" : "4dc9511a14a7d017fee35746"  
  5.     } ,   
  6.     "chunkSize" : 262144 ,   
  7.     "length" : 22672 ,   
  8.     "md5" : "1462a6cfa27669af1d8d21c2d7dd1f8b" ,   
  9.     "filename" : "mkyong-java-image" ,   
  10.     "contentType" :  null  ,   
  11.     "uploadDate" :   
  12.     {   
  13.         "$date" : "2011-05-10T14:52:10Z"  
  14.     } ,   
  15.     "aliases" :  null   
  16. }  

3. Print all saved images

Code snippets to get all the saved files from MongoDB and iterate it with DBCursor.

译:下面代码片断,从MongoDB中获取所有保存的文件,并用数据库游标迭代输出。

Java代码  
  1. GridFS gfsPhoto = new GridFS(db, "photo");  
  2. DBCursor cursor = gfsPhoto.getFileList();  
  3. while (cursor.hasNext()) {  
  4.     System.out.println(cursor.next());  
  5. }  

4. Save into another image

Code snippets to get an image file from MongoDB and output it to another image file.

译:下面代码片断,从MongoDB中获取一个图片文件并输出(生成另一个图片)。

Java代码  
  1. String newFileName = "mkyong-java-image";  
  2. GridFS gfsPhoto = new GridFS(db, "photo");  
  3. GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);  
  4. imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); //output to new file  

5. Delete image

Code snippets to delete an image file.

译:下面代码片断,删除一个图片文件。

Java代码  
  1. String newFileName = "mkyong-java-image";  
  2. GridFS gfsPhoto = new GridFS(db, "photo");  
  3. 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代码  
  1. package com.mkyong.core;  
  2.    
  3. import java.io.File;  
  4. import java.io.IOException;  
  5. import java.net.UnknownHostException;  
  6. import com.mongodb.DB;  
  7. import com.mongodb.DBCollection;  
  8. import com.mongodb.DBCursor;  
  9. import com.mongodb.Mongo;  
  10. import com.mongodb.MongoException;  
  11. import com.mongodb.gridfs.GridFS;  
  12. import com.mongodb.gridfs.GridFSDBFile;  
  13. import com.mongodb.gridfs.GridFSInputFile;  
  14.    
  15. /** 
  16.  * Java MongoDB : Save image example 
  17.  *  
  18.  */  
  19.    
  20. public class SaveImageApp {  
  21.     public static void main(String[] args) {  
  22.    
  23.         try {  
  24.    
  25.             Mongo mongo = new Mongo("localhost", 27017);  
  26.             DB db = mongo.getDB("imagedb");  
  27.             DBCollection collection = db.getCollection("dummyColl");  
  28.    
  29.             String newFileName = "mkyong-java-image";  
  30.    
  31.             File imageFile = new File("c:\\JavaWebHosting.png");  
  32.    
  33.             // create a "photo" namespace  
  34.             GridFS gfsPhoto = new GridFS(db, "photo");  
  35.    
  36.             // get image file from local drive  
  37.             GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile);  
  38.    
  39.             // set a new filename for identify purpose  
  40.             gfsFile.setFilename(newFileName);  
  41.    
  42.             // save the image file into mongoDB  
  43.             gfsFile.save();  
  44.    
  45.             // print the result  
  46.             DBCursor cursor = gfsPhoto.getFileList();  
  47.             while (cursor.hasNext()) {  
  48.                 System.out.println(cursor.next());  
  49.             }  
  50.    
  51.             // get image file by it‘s filename  
  52.             GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);  
  53.    
  54.             // save it into a new image file  
  55.             imageForOutput.writeTo("c:\\JavaWebHostingNew.png");  
  56.    
  57.             // remove the image file from mongoDB  
  58.             gfsPhoto.remove(gfsPhoto.findOne(newFileName));  
  59.    
  60.             System.out.println("Done");  
  61.    
  62.         } catch (UnknownHostException e) {  
  63.             e.printStackTrace();  
  64.         } catch (MongoException e) {  
  65.             e.printStackTrace();  
  66.         } catch (IOException e) {  
  67.             e.printStackTrace();  
  68.         }  
  69.    
  70.     }  
  71. }  

 

At the end of the program, a new image file is created in “c:\\JavaWebHostingNew.png“.

译:执行程序之后,会创建一个新的图片文件“c:\\JavaWebHostingNew.png“。

 

Reference

本类排行

今日推荐

热门手游