from:
GridFS简介
GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件。
GridFS使用
MongoDB提供了一个命令行工具mongofiles可以来处理GridFS,在bin目录下。
列出所有文件:
mongofiles list
上传一个文件:
mongofiles put xxx.txt
下载一个文件:
mongofiles get xxx.txt
查找文件:
mongofiles search xxx //会查找所有文件名中包含“xxx”的文件
mongofiles list xxx //会查找所有文件名以“xxx”为前缀的文件
参数说明:
–d 指定数据库 ,默认是fs,Mongofiles list –d testGridfs
-u –p 指定用户名,密码
-h 指定主机
-port 指定主机端口
-c 指定集合名,默认是fs
-t 指定文件的MIME类型,默认会忽略
使用MongoVUE来查看,管理GridFS
MongoVUE地址:
MongoVUE是个免费软件,但超过15天后功能受限。可以通过删除以下注册表项来解除限制:
[HKEY_CURRENT_USER\Software\Classes\CLSID\{B1159E65-821C3-21C5-CE21-34A484D54444}\4FF78130]
把这个项下的值全删掉就可以了。
用java驱动上传下载文件:
下载地址:
官方的文档貌似不是最新的,不过通过查看api来使用也不困骓。
以下代码基于mongo-2.7.3.jar
001 | import java.io.FileInputStream; |
002 | import java.io.IOException; |
003 | import java.io.InputStream; |
004 | import java.net.UnknownHostException; |
005 | import java.security.NoSuchAlgorithmException; |
007 | import com.mongodb.BasicDBObject; |
008 | import com.mongodb.DB; |
009 | import com.mongodb.DBCollection; |
010 | import com.mongodb.DBObject; |
011 | import com.mongodb.Mongo; |
012 | import com.mongodb.MongoException; |
013 | import com.mongodb.gridfs.GridFS; |
014 | import com.mongodb.gridfs.GridFSDBFile; |
015 | import com.mongodb.gridfs.GridFSInputFile; |
020 |
DBCollection collection; |
023 |
String mongoDBHost = "127.0.0.1" ; |
024 |
int mongoDBPort = 27017 ; |
025 |
String dbName = "testGridfs" ; |
026 |
String collectionName = "fs" ; |
028 |
public static void main(String[] args) throws MongoException, IOException, NoSuchAlgorithmException { |
031 |
String fileName = "F:/CPU.txt" ; |
032 |
String name = "CPU.txt" ; |
035 |
t.save( new FileInputStream(fileName), name); |
039 |
GridFSDBFile gridFSDBFile = t.getByFileName(name); |
040 |
if (gridFSDBFile != null ){ |
041 |
System.out.println( "filename:" + gridFSDBFile.getFilename()); |
042 |
System.out.println( "md5:" + gridFSDBFile.getMD5()); |
043 |
System.out.println( "length:" + gridFSDBFile.getLength()); |
044 |
System.out.println( "uploadDate:" + gridFSDBFile.getUploadDate()); |
046 |
System.out.println( "--------------------------------------" ); |
047 |
gridFSDBFile.writeTo(System.out); |
049 |
System.out.println( "can not get file by name:" + name); |
053 |
public Test() throws UnknownHostException, MongoException, NoSuchAlgorithmException { |
058 |
public Test(String mongoDBHost, int mongoDBPort, String dbName, |
059 |
String collectionName) throws UnknownHostException, MongoException, NoSuchAlgorithmException { |
060 |
this .mongoDBHost = mongoDBHost; |
061 |
this .mongoDBPort = mongoDBPort; |
062 |
this .dbName = dbName; |
063 |
this .collectionName = collectionName; |
068 |
private void _init() throws UnknownHostException, MongoException, NoSuchAlgorithmException{ |
069 |
connection = new Mongo(mongoDBHost, mongoDBPort); |
070 |
db = connection.getDB(dbName); |
071 |
collection = db.getCollection(collectionName); |
072 |
myFS = new GridFS(db); |
076 |
* 用给出的id,保存文件,透明处理已存在的情况 |
077 |
* id 可以是string,long,int,org.bson.types.ObjectId 类型 |
081 |
public void save(InputStream in, Object id){ |
082 |
DBObject query = new BasicDBObject( "_id" , id); |
083 |
GridFSDBFile gridFSDBFile = myFS.findOne(query); |
085 |
if (gridFSDBFile != null ) |
088 |
GridFSInputFile gridFSInputFile = myFS.createFile(in); |
089 |
gridFSInputFile.save(); |
098 |
public GridFSDBFile getById(Object id){ |
099 |
DBObject query = new BasicDBObject( "_id" , id); |
100 |
GridFSDBFile gridFSDBFile = myFS.findOne(query); |
109 |
public GridFSDBFile getByFileName(String fileName){ |
110 |
DBObject query = new BasicDBObject( "filename" , fileName); |
111 |
GridFSDBFile gridFSDBFile = myFS.findOne(query); |