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();

}

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐