Java MongoDB:画像の保存の例
このチュートリアルでは、GridFS APIを介してsave an imageファイルをMongoDBに取り込む方法を示します。 GridFS APIは、ビデオファイルや音楽ファイルなどの他のバイナリファイルも提供できます。
Note
詳細な説明については、このMongoDB GridFS manualをお読みください。
1. 画像を保存
「スニペット」をコード化して、画像ファイルを「photo」名前空間の下のMongoDBに保存し、保存した画像に新しい「ファイル名」を割り当てます。
String newFileName = "example-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. 画像を取得する
「ファイル名」で保存された画像を取得するためのコードスニペット。
String newFileName = "example-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); System.out.println(imageForOutput);
出力では、画像は次のJSON形式で保存されます。
{ "_id" : { "$oid" : "4dc9511a14a7d017fee35746" } , "chunkSize" : 262144 , "length" : 22672 , "md5" : "1462a6cfa27669af1d8d21c2d7dd1f8b" , "filename" : "example-java-image" , "contentType" : null , "uploadDate" : { "$date" : "2011-05-10T14:52:10Z" } , "aliases" : null }
3. 保存したすべての画像を印刷する
MongoDBから保存されたすべてのファイルを取得し、DBCursorで繰り返し処理するコードスニペット。
GridFS gfsPhoto = new GridFS(db, "photo"); DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); }
4. 別の画像に保存する
MongoDBから画像ファイルを取得して別の画像ファイルに出力するコードスニペット。
String newFileName = "example-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); imageForOutput.writeTo("c:\\JavaWebHostingNew.png"); //output to new file
5. 画像を削除
画像ファイルを削除するためのコードスニペット。
String newFileName = "example-java-image"; GridFS gfsPhoto = new GridFS(db, "photo"); gfsPhoto.remove(gfsPhoto.findOne(newFileName));
完全な例
Java MongoDB GridFS APIを介して画像を操作する完全な例。 説明についてはコメントを参照してください。
package com.example.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 = "example-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(); } } }
プログラムの最後に、「c:\JavaWebHostingNew.png」に新しい画像ファイルが作成されます。