Save (Insert) image in mongoDb in java, Retrieve image, Display it and save it in local hard disk



Contents of page >
  • 1) Program to Save (Insert) image in mongoDb in java, Retrieve image, Display it and save it in local hard disk (E:\ drive)
  • 2) Program to Delete Image From MongoDb in java >


1) Program to
  • Save image (Insert) in MongoDb in java
  • Retrieve image (Fetch) and Display retrieved image document in MongoDb in java
  • Save retrieved image into local hard disk (E:\ drive)
package imageMongoDB;
/**
* Write a program to >
*   - Save image in MongoDb in java

*   - Retrieve image and Display retrieved image document in MongoDb in java

*   - Save retrieved image into local hard disk (E:\ drive)
*/
import java.io.File;
import java.util.List;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
public class SaveDisplayRetriveImageInMongoDb {
   public static void main(String[] args) throws Exception {
          /* Connect to MongoDB */
          MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
         
          /* Connect to DATABASE */
          //If in case database doesn't exists, it will be created at runtime
          DB db = mongoClient.getDB("mydb");
                      
          /* Get the COLLECTION (TABLE) */
          //If in case collection (Table) doesn't exists, it will be created at runtime
          DBCollection dbCollection = db.getCollection("ImageCollection");
          /* Step 1- Create imageFile */
          System.out.println("Step 1- Create imageFile");
          File imageFile = new File("E:/tiger.jpg");
          /* Step 2- create gridFS object */
          System.out.println("Step 2- create gridFS object");
          GridFS gridFS = new GridFS(db);
          /* Step 3- Create GridFSInputFile object from imageFile */
          System.out.println("Step 3- Create GridFSInputFile object from imageFile");
          GridFSInputFile gridFSInputFile = gridFS.createFile(imageFile);
          /* Step 4- Set name for image in MongoDB */
          System.out.println("Step 4- Set name for image in MongoDB");
          gridFSInputFile.setFilename("tiger.jpg");
         /** Step 5- Save image in collection in MongoDB */
          System.out.println("\nStep 5- Save image in collection in MongoDB");
          gridFSInputFile.save();
         /** Step 6- Display Image document stored in collection in MongoDB */
          System.out.println("\nStep 6- Display Image document stored in collection in MongoDB");
          DBCursor cursor = gridFS.getFileList();
          while (cursor.hasNext()) {
                 System.out.println(cursor.next());
          }
         /** Step 7- Retrieve image from collection in MongoDB */
          System.out.println("\nStep 7- Retrieve image from collection in MongoDB");
          List<GridFSDBFile> imageForOutput = gridFS.find("tiger.jpg");
         /** Step 8- Save retrieved image into local hard disk drive E:\drive*/
          System.out.println("\nStep 8- Save retrieved image into local drive (E: drive) ");
          imageForOutput.get(0).writeTo("E:/tiperImageDOWNLOADEDfromMongoDB.jpg");
          System.out.println("retrieved image saved in E:/tiperImageDOWNLOADEDfromMongoDB.jpg");
   }
}
/* OUTPUT
Step 1- Create imageFile
Step 2- create gridFS object
Step 3- Create GridFSInputFile object from imageFile
Step 4- Set name for image in MongoDB
Step 5- Save image in collection in MongoDB
Step 6- Display Image document stored in collection in MongoDB
{ "filename" : "tiger.jpg" , "aliases" :  null  , "chunkSize" : 261120 , "uploadDate" : { "$date" : "2016-12-23T11:15:34.128Z"} , "length" : 13008 , "_id" : { "$oid" : "585d0756e1bb4d38204dd6ab"} , "contentType" :  null  , "md5" : "5238c0366ae26e896c0851390ad7dce8"}
Step 7- Retrieve image from collection in MongoDB
Step 8- Save retrieved image into local drive (E: drive)
retrieved image saved in E:/tiperImageDOWNLOADEDfromMongoDB.jpg
*/

2) Program to Delete Image From MongoDb in java >
package imageMongoDB;
/**
* Write a program to Delete Image From MongoDb in java
*/
import java.io.File;
import java.util.List;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
public class DeleteImageFromMongoDb {
   public static void main(String[] args) throws Exception {
          /* Connect to MongoDB */
          MongoClient mongoClient = new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
         
          /* Connect to DATABASE */
          //If in case database doesn't exists, it will be created at runtime
          DB db = mongoClient.getDB("mydb");
                      
          /* Get the COLLECTION (TABLE) */
          //If in case collection (Table) doesn't exists, it will be created at runtime
          DBCollection dbCollection = db.getCollection("ImageCollection");
         
          /* Step 1- Create imageFile */
          System.out.println("Step 1- Create imageFile");
          File imageFile = new File("E:/tiger.jpg");
          /* Step 2- create gridFS object */
          System.out.println("Step 2- create gridFS object");
          GridFS gridFS = new GridFS(db);
          /* Step 3- Create GridFSInputFile object from imageFile */
          System.out.println("Step 3- Create GridFSInputFile object from imageFile");
          GridFSInputFile gridFSInputFile = gridFS.createFile(imageFile);
          /* Step 4- Set name for image in MongoDB */
          System.out.println("Step 4- Set name for image in MongoDB");
          gridFSInputFile.setFilename("tiger.jpg");
          /* Step 5- Save image in collection in MongoDB */
          System.out.println("Step 5- Save image in collection in MongoDB");
          gridFSInputFile.save();
          /* Step 6- Display Image stored in collection in MongoDB */
          System.out.println("Step 6- Display Image stored in collection in MongoDB");
          DBCursor cursor = gridFS.getFileList();
          while (cursor.hasNext()) {
                 System.out.println(cursor.next());
          }
          /* Step 7- Retrieve image from collection in MongoDB */
          System.out.println("Step 7- Retrieve image from collection in MongoDB");
          List<GridFSDBFile> imageForOutput = gridFS.find("tiger.jpg");
          /** Step 8- Delete/Remove the image from collection in MongoDB */
          System.out.println("\nStep 8- Delete/Remove the image from collection in MongoDB");
          gridFS.remove(imageForOutput.get(0));
   }
}
/* OUTPUT
Step 1- Create imageFile
Step 2- create gridFS object
Step 3- Create GridFSInputFile object from imageFile
Step 4- Set name for image in MongoDB
Step 5- Save image in collection in MongoDB
Step 6- Display Image stored in collection in MongoDB
{ "filename" : "tiger.jpg" , "aliases" :  null  , "chunkSize" : 261120 , "uploadDate" : { "$date" : "2016-12-23T11:17:47.102Z"} , "length" : 13008 , "_id" : { "$oid" : "585d07dbe1bb4d3f2c5bc59b"} , "contentType" :  null  , "md5" : "5238c0366ae26e896c0851390ad7dce8"}
Step 7- Retrieve image from collection in MongoDB
Step 8- Delete/Remove the image from collection in MongoDB
*/

Download >
Now, it’s time to Download maven Project :) Click here.

Read : How to Import Maven project in eclipse

OR



Summary -
So in this MongoDB tutorial we learned with example and program to
Save image in mongoDb in java, Retrieve image, Display it and save it in local hard disk.


Having any doubt? or you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter. You may join our fbGroup or linkedInGroup as well.


RELATED LINKS>





eEdit
Must read for you :