<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Java MongoDB:使用 GridFS API 獲取/保存圖像 > 原文: [https://howtodoinjava.com/mongodb/java-mongodb-getsave-image-using-gridfs-apis/](https://howtodoinjava.com/mongodb/java-mongodb-getsave-image-using-gridfs-apis/) 在先前的教程中,我們了解了 [MongoDB 基礎知識](//howtodoinjava.com/nosql/introduction-to-mongodb-why-mongodb/ "Introduction to MongoDB: Why MongoDB?")和[在 Windows](//howtodoinjava.com/nosql/mongodb/how-to-install-mongodb-on-windows/ "How to Install MongoDB on Windows") 中安裝 MongoDB 的知識,以及有關[在 MongoDB 中插入文檔](//howtodoinjava.com/nosql/mongodb/java-mongodb-insert-documents-in-collection-examples/ "Java MongoDB : Insert Document(s) in Collection Examples")和[從 MongoDB 選擇文檔](//howtodoinjava.com/nosql/mongodb/mongodb-selectqueryfind-documents-examples/ "MongoDB : Select/Query/Find Document(s) Examples")的知識。 在本教程中,我僅提供一些代碼示例,這些代碼示例將在您必須使用圖像或其他二進制數據類型來從 MongoDB 存儲/檢索/刪除此類數據時提供幫助。 對于 CRUD 操作,我們將使用 **MongoDB 的 GridFS API** 。 在理想情況下,GridFS 是**規范,用于存儲和檢索超出 BSON 文檔大小限制 16MB 的文件**。 而不是將文件存儲在單個文檔中, **GridFS 將文件劃分為多個部分或大塊,并將這些大塊中的每個存儲為單獨的文檔**。 默認情況下,GridFS 將塊大小限制為 255k。 GridFS 使用兩個集合來存儲文件。 一個集合存儲文件塊,另一個集合存儲文件元數據。 當您查詢 GridFS 存儲中的文件時,驅動或客戶端將根據需要重新組裝塊。 **您可以對通過 GridFS** 存儲的文件執行范圍查詢。 您還可以從文件的任意部分訪問信息,從而可以“跳入”視頻或音頻文件的中間。 GridFS 不僅可用于存儲超過 16MB 的文件,而且還可用于存儲要訪問**的任何文件,而不必將整個文件加載到內存**中。 ```java Code Examples List Save an image into MongoDB Get/Retrieve an image from MongoDB Get/Retrieve all images from MongoDB Save the retrieved image into local file-system Delete an image from MongoDB ``` 為了進行測試,我將圖像存儲在名為`DemoImage.png`的臨時位置(`c:`驅動器)中。 我將此圖像存儲到 MongoDB 中。 ## 將圖像保存到 MongoDB ```java private static void saveImageIntoMongoDB(DB db) throws IOException { String dbFileName = "DemoImage"; File imageFile = new File("c:\\DemoImage.png"); GridFS gfsPhoto = new GridFS(db, "photo"); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(dbFileName); gfsFile.save(); } ``` ## 從 MongoDB 獲取/檢索圖像 ```java private static void getSingleImageExample(DB db) { String newFileName = "c:/DemoImage"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); System.out.println(imageForOutput); } Output: { "_id" : { "$oid" : "53cff8d736414e8af4a4f0b8"} , "chunkSize" : 262144 , "length" : 138855 , "md5" : "b75f77c16c3ac6472365c06cde15d0da" , "filename" : "DemoImage" , "contentType" : null , "uploadDate" : { "$date" : "2014-07-23T18:03:03.403Z"} , "aliases" : null } ``` ## 從 MongoDB 獲取/檢索所有圖像 ```java private static void listAllImages(DB db) { GridFS gfsPhoto = new GridFS(db, "photo"); DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); } } Output: { "_id" : { "$oid" : "53cff8d736414e8af4a4f0b8"} , "chunkSize" : 262144 , "length" : 138855 , "md5" : "b75f77c16c3ac6472365c06cde15d0da" , "filename" : "DemoImage" , "contentType" : null , "uploadDate" : { "$date" : "2014-07-23T18:03:03.403Z"} , "aliases" : null } ``` ## 將檢索到的圖像保存到本地文件系統中 ```java private static void saveToFileSystem(DB db) throws IOException { String dbFileName = "DemoImage"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(dbFileName); imageForOutput.writeTo("c:/DemoImageNew.png"); } ``` ## 從 MongoDB 刪除圖像 ```java private static void deleteImageFromMongoDB(DB db) { String dbFileName = "DemoImage"; GridFS gfsPhoto = new GridFS(db, "photo"); gfsPhoto.remove(gfsPhoto.findOne(dbFileName)); } ``` ## 完整的示例代碼 以下是構建以上簡短示例的完整信息。 隨意玩。 ```java package examples.mongodb.crud; import java.io.File; import java.io.IOException; import com.mongodb.DB; import com.mongodb.DBCursor; import com.mongodb.MongoClient; import com.mongodb.gridfs.GridFS; import com.mongodb.gridfs.GridFSDBFile; import com.mongodb.gridfs.GridFSInputFile; public class MongoDBBinaryExample { public static void main(String[] args) throws IOException { MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("howtodoinjava"); //Save a image in DB saveImageIntoMongoDB(db); //Get a image from DB getSingleImageExample(db); //Get all images from DB listAllImages(db); saveToFileSystem(db); //Delete images from DB deleteImageFromMongoDB(db); //Verifying if image was deleted or not getSingleImageExample(db); } private static void saveImageIntoMongoDB(DB db) throws IOException { String dbFileName = "DemoImage"; File imageFile = new File("c:\\DemoImage.png"); GridFS gfsPhoto = new GridFS(db, "photo"); GridFSInputFile gfsFile = gfsPhoto.createFile(imageFile); gfsFile.setFilename(dbFileName); gfsFile.save(); } private static void getSingleImageExample(DB db) { String newFileName = "c:/DemoImage"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName); System.out.println(imageForOutput); } private static void listAllImages(DB db) { GridFS gfsPhoto = new GridFS(db, "photo"); DBCursor cursor = gfsPhoto.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); } } private static void saveToFileSystem(DB db) throws IOException { String dbFileName = "DemoImage"; GridFS gfsPhoto = new GridFS(db, "photo"); GridFSDBFile imageForOutput = gfsPhoto.findOne(dbFileName); imageForOutput.writeTo("c:/DemoImageNew.png"); } private static void deleteImageFromMongoDB(DB db) { String dbFileName = "DemoImage"; GridFS gfsPhoto = new GridFS(db, "photo"); gfsPhoto.remove(gfsPhoto.findOne(dbFileName)); } } Output: { "_id" : { "$oid" : "53cff8d736414e8af4a4f0b8"} , "chunkSize" : 262144 , "length" : 138855 , "md5" : "b75f77c16c3ac6472365c06cde15d0da" , "filename" : "DemoImage" , "contentType" : null , "uploadDate" : { "$date" : "2014-07-23T18:03:03.403Z"} , "aliases" : null } { "_id" : { "$oid" : "53cff8d736414e8af4a4f0b8"} , "chunkSize" : 262144 , "length" : 138855 , "md5" : "b75f77c16c3ac6472365c06cde15d0da" , "filename" : "DemoImage" , "contentType" : null , "uploadDate" : { "$date" : "2014-07-23T18:03:03.403Z"} , "aliases" : null } null ``` 這就是本簡單教程的全部內容。 希望它能幫助有需要的人。 **祝您學習愉快!** **參考**:[http://docs.mongodb.org/manual/core/gridfs/](http://docs.mongodb.org/manual/core/gridfs/ "gridfs")
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看