package test;
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
/**
* 将一系列图片文件存储到MongoDB中
* @author Kaka
*
*/
public class MongFileTest {
String path="D:\\img\\original";
File[]listFile(){
File directory=new File(path);
return directory.listFiles();
}
public static void main(String[] args) throws MongoException, IOException {
//
MongFileTest mft=new MongFileTest();
//连接服务器
Mongo mongo=new Mongo("127.0.0.1", 27017);
//数据库
DB db=mongo.getDB("files");
//存储fs的根节点
GridFS gridFS=new GridFS(db,"pictures");
//列出文件下目录
File []files=mft.listFile();
//遍历存储近MongoDB
for (File file : files) {
System.out.println("save "+file.getAbsoluteFile());
GridFSInputFile gfs=gridFS.createFile(file);
gfs.put("filename", file.getName());
gfs.put("path", file.getAbsoluteFile());
gfs.put("contentType", file.getName().substring(file.getName().lastIndexOf(".")));
gfs.save();
}
//检索
List<GridFSDBFile> list =gridFS.find("201008171282033626670.jpg");
for (GridFSDBFile dbFile : list) {
String name="t.jpg";
dbFile.writeTo(name);
System.out.println("out put to "+name);
name="t"+name;
}
}
}
运行一下,对不起,报错
Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class java.io.File
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:185)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:119)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:65)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:176)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:134)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:129)
at com.mongodb.DBCollection.save(DBCollection.java:418)
at com.mongodb.gridfs.GridFSFile.save(GridFSFile.java:39)
at com.mongodb.gridfs.GridFSInputFile.save(GridFSInputFile.java:67)
at test.MongFileTest.main(MongFileTest.java:42)
错误还挺怪异,can't serialize class java.io.File,晕头转向。。。。。
根本找不着问题是在哪...注(这里使用的mongo.jar是2.1版)
Google查好像也没人出这个错。考虑换个jar包...
换了2.0和1.4
最后1.4报错比较靠谱...
报错是:"GridFSFile don't know about key [" + ***+ "] converting to metadata "
看了下它的源码,原来文件存储不是你想往里放什么key-value就放什么kv的,指定类型:
final static Set<String> VALID_FIELDS = Collections.unmodifiableSet( new HashSet( Arrays.asList( new String[]{
"_id" , "filename" , "contentType" , "length" , "chunkSize" ,
"uploadDate" , "aliases" , "metadata" , "md5"
} ) ) );
path去掉就ok了~~~
文档还是比较差劲啊~~~
import java.io.File;
import java.io.IOException;
import java.util.List;
import com.mongodb.DB;
import com.mongodb.Mongo;
import com.mongodb.MongoException;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
/**
* 将一系列图片文件存储到MongoDB中
* @author Kaka
*
*/
public class MongFileTest {
String path="D:\\img\\original";
File[]listFile(){
File directory=new File(path);
return directory.listFiles();
}
public static void main(String[] args) throws MongoException, IOException {
//
MongFileTest mft=new MongFileTest();
//连接服务器
Mongo mongo=new Mongo("127.0.0.1", 27017);
//数据库
DB db=mongo.getDB("files");
//存储fs的根节点
GridFS gridFS=new GridFS(db,"pictures");
//列出文件下目录
File []files=mft.listFile();
//遍历存储近MongoDB
for (File file : files) {
System.out.println("save "+file.getAbsoluteFile());
GridFSInputFile gfs=gridFS.createFile(file);
gfs.put("filename", file.getName());
gfs.put("path", file.getAbsoluteFile());
gfs.put("contentType", file.getName().substring(file.getName().lastIndexOf(".")));
gfs.save();
}
//检索
List<GridFSDBFile> list =gridFS.find("201008171282033626670.jpg");
for (GridFSDBFile dbFile : list) {
String name="t.jpg";
dbFile.writeTo(name);
System.out.println("out put to "+name);
name="t"+name;
}
}
}
运行一下,对不起,报错
Exception in thread "main" java.lang.IllegalArgumentException: can't serialize class java.io.File
at org.bson.BSONEncoder._putObjectField(BSONEncoder.java:185)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:119)
at org.bson.BSONEncoder.putObject(BSONEncoder.java:65)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:176)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:134)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:129)
at com.mongodb.DBCollection.save(DBCollection.java:418)
at com.mongodb.gridfs.GridFSFile.save(GridFSFile.java:39)
at com.mongodb.gridfs.GridFSInputFile.save(GridFSInputFile.java:67)
at test.MongFileTest.main(MongFileTest.java:42)
错误还挺怪异,can't serialize class java.io.File,晕头转向。。。。。
根本找不着问题是在哪...注(这里使用的mongo.jar是2.1版)
Google查好像也没人出这个错。考虑换个jar包...
换了2.0和1.4
最后1.4报错比较靠谱...
报错是:"GridFSFile don't know about key [" + ***+ "] converting to metadata "
看了下它的源码,原来文件存储不是你想往里放什么key-value就放什么kv的,指定类型:
final static Set<String> VALID_FIELDS = Collections.unmodifiableSet( new HashSet( Arrays.asList( new String[]{
"_id" , "filename" , "contentType" , "length" , "chunkSize" ,
"uploadDate" , "aliases" , "metadata" , "md5"
} ) ) );
path去掉就ok了~~~
文档还是比较差劲啊~~~