MongoDB数据库保存Java端图片文件

MongoDB数据库保存Java端图片文件

🗨

MongoDB 与传统的关系型数据库一样能保存图片或文件到数据库,不过操作的方法有点特殊,它借助了 GridFS 来保存文件到数据库,比如图片,视频,音乐等文件都可以。

关于GridFS 的介绍,可以参考这里:  http://docs.mongodb.org/manual/core/gridfs/

一般来说对文件册操作主要包括以下几方面:
1. 保存文件到mongoDB.
2. 获取所有的文件列表
3. 从MongoDB 中得到文件,并输出保存到硬盘上
4. 从MongoDB中删除数据

1. 保存文件到mongoDB.

程序代码 程序代码

String newFileName = "yihaomen-java-image";
File imageFile = new File("c:\\images\\Winter.jpg");
// 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();             



2. 从MongoDB 中得到文件,并输出保存到硬盘上

程序代码 程序代码

GridFS gfsPhoto = new GridFS(db, "photo");
DBCursor cursor = gfsPhoto.getFileList();
while (cursor.hasNext()) {
    System.out.println(cursor.next());
}



3. 从MongoDB 中得到文件,并输出保存到硬盘上

程序代码 程序代码

GridFS gfsPhoto = new GridFS(db, "photo");
GridFSDBFile imageForOutput = gfsPhoto.findOne(newFileName);
// save it into a new image file
imageForOutput.writeTo("c:\\images\\Winter-From-Mongodb.jpg");



4. 删除文件数据

程序代码 程序代码

String newFileName = yiahomen-java-image";
GridFS gfsPhoto = new GridFS(db, "photo");
gfsPhoto.remove(gfsPhoto.findOne(newFileName));



整个测试代码如下:

程序代码 程序代码

package com.yihaomen.mongodb.process.image;

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;

public class MongoDBImage {
    
    public static void main(String[] args) {        
        try {

            Mongo mongo = new Mongo("localhost", 27017);
            DB db = mongo.getDB("yihaomen");            
            DBCollection collection = db.getCollection("MyImage");
            String newFileName = "yihaomen-java-image";
            File imageFile = new File("c:\\images\\Winter.jpg");
            // 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:\\images\\Winter-From-Mongodb.jpg");
            // 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();
        }
    }
}

频道:Java