我想在mongoDB中存储一个文件并从那里检索它来处理它。 我用GRIDFS来存储它
Mongo mongo = new Mongo("localhost", 27017);
DB db = mongo.getDB("HelloWorldDB");
DBCollection collection = db.getCollection("Uploaded_Exclusion_files");
GridFS gridfs = new GridFS(db, "downloads");
GridFSInputFile gfsFile = gridfs.createFile(new File(fileName));
gfsFile.setFilename("filename");
gfsFile.save();
BasicDBObject info = new BasicDBObject();
info.put("name", "MongoDB");
info.put("fileName", "filename");
info.put("rawName", "fromJSON.csv");
info.put("rawPath", "d:/fromJSON.csv");
//
// Let's store our document to MongoDB
//
collection.insert(info, WriteConcern.SAFE);
DBCursor cursor= collection.find(info);
while(cursor.hasNext()){
System.out.println("Result "+cursor.next());
}
System.out.println("Done");
但是在检索文件时,我不知道将它作为文件做什么并进行处理。
GridFSDBFile gfsFileOut = (GridFSDBFile) gridfs.findOne("filename");
System.out.println(gfsFileOut.getInputStream());
//##### WHAT TO DO TO GET THE FILE
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(<#####I WANT TO PASS THE FILE TO PROCESS IT######>));
// ...
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
我想删除服务器中的文件创建部分,只想存储在mongodb中并检索它以进行处理。
答案 0 :(得分:2)
我得到了使用GridFS从MongoDB获取文件的答案
GridFSDBFile gfsFileOut = (GridFSDBFile) gridfs.findOne(rawName);
System.out.println(gfsFileOut.getInputStream());
InputStream is = gfsFileOut.getInputStream();
// Reading the excel file
List<String> list = null;
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));