使用Java&在MongoDB中存储和检索文件网格文件 [英] Storing and retriving file in MongoDB using Java & GridFs
本文介绍了使用Java&在MongoDB中存储和检索文件网格文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将文件存储在mongoDB中,然后从那里检索文件以进行处理. 我已经用GRIDFS来存储它了
I would like to store a file in mongoDB and retrieve it from there to process it. I have used the GRIDFS to store it
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");
但是在检索文件时,我不知道将其作为文件进行处理的方式.
but at the time of retrieving the file I don't l know what to make it as a file and process it.
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中并检索它以进行处理.
I want to remove the file creation part in server and only wants to store in mongodb and retrieve it for processing it.
推荐答案
我得到了使用GridFS从MongoDB中获取文件的答案
I got the answer to Get the file from MongoDB using GridFS
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"));
这篇关于使用Java&在MongoDB中存储和检索文件网格文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文