List of usage examples for com.mongodb.gridfs GridFSInputFile getMD5
@Deprecated
public String getMD5()
From source file:UnitTest3.java
License:Open Source License
public static int testgridfs() { long time1;/*from www. jav a 2 s. c o m*/ long time2; long time3; long time4; try { MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("JFileDB"); // TODO JFileMetaDataTable should be in MetaDB database DBCollection collection = db.getCollection("JFileMetaDataTable"); String newFileName = "com.dilmus.scabi.testdata.in.App.class"; File jFile = new File("/home/anees/workspace/testdata/in/App.class"); // create a JFileTable namespace GridFS gfsj = new GridFS(db, "JFileTable"); // get file from local drive GridFSInputFile gfsFile = gfsj.createFile(jFile); // set a new filename for identify purpose gfsFile.setFilename(newFileName); gfsFile.setContentType("class"); // jar, zip, war // save the image file into mongoDB gfsFile.save(); // Let's create a new JSON document with some "metadata" information BasicDBObject info = new BasicDBObject(); info.put("DBHost", "localhost"); info.put("DBPort", "27017"); info.put("JFileName", newFileName); info.put("JFileID", gfsFile.getId()); info.put("JFileMD5", gfsFile.getMD5()); collection.insert(info, WriteConcern.ACKNOWLEDGED); // print the result DBCursor cursor = gfsj.getFileList(); while (cursor.hasNext()) { System.out.println(cursor.next()); } DBCursor cursor2 = collection.find(); while (cursor2.hasNext()) { System.out.println(cursor2.next()); } // get file by it's filename GridFSDBFile jForOutput = gfsj.findOne(newFileName); // save it into a new image file jForOutput.writeTo("/home/anees/workspace/testdata/out/AppOut.class"); // remove the file from mongoDB // gfsj.remove(gfsj.findOne(newFileName)); System.out.println("Done"); mongo.close(); } catch (UnknownHostException e) { e.printStackTrace(); } catch (MongoException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return 0; }
From source file:org.nuxeo.ecm.core.storage.mongodb.GridFSBinaryManager.java
License:Apache License
@Override protected Binary getBinary(InputStream in) throws IOException { // save the file to GridFS GridFSInputFile inputFile = gridFS.createFile(in, true); inputFile.save();//from w ww .j av a 2 s. c o m // now we know length and digest String digest = inputFile.getMD5(); // if the digest is already known then reuse it instead GridFSDBFile dbFile = gridFS.findOne(digest); if (dbFile == null) { // no existing file, set its filename as the digest inputFile.setFilename(digest); inputFile.save(); } else { // file already existed, no need for the temporary one gridFS.remove(inputFile); } return new GridFSBinary(digest, blobProviderId); }