List of usage examples for com.mongodb.gridfs GridFS createFile
public GridFSInputFile createFile(final String filename)
From source file:UnitTest3.java
License:Open Source License
public static int testgridfs() { long time1;/*ww w . j av a 2s. 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:calliope.db.MongoConnection.java
License:Open Source License
/** * Store an image in the database//from ww w. ja va 2s. c o m * @param collName name of the image collection * @param docID the docid of the resource * @param data the image data to store * @throws AeseException */ @Override public void putImageToDb(String collName, String docID, byte[] data) throws AeseException { docIDCheck(collName, docID); GridFS gfs = new GridFS(db, collName); GridFSInputFile file = gfs.createFile(data); file.setFilename(docID); file.save(); }
From source file:com.andreig.jetty.GridfsServlet.java
License:GNU General Public License
@SuppressWarnings("unused") @Override// w ww . j a v a 2 s.c o m protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.fine("doPost()"); if (!can_write(req)) { res.sendError(SC_UNAUTHORIZED); return; } InputStream tmp = req.getInputStream(); InputStream is = new BufferedInputStream(tmp); String db_name = req.getParameter("dbname"); String bucket_name = req.getParameter("bucketname"); if (db_name == null || bucket_name == null) { String names[] = req2mongonames(req); if (names != null) { db_name = names[0]; bucket_name = names[1]; } if (db_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } } if (bucket_name == null) bucket_name = "fs"; String file_name = req.getParameter("filename"); if (file_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } DB db = mongo.getDB(db_name); String fs_cache_key = db_name + bucket_name; GridFS fs = fs_cache.get(fs_cache_key); if (fs == null) { fs = new GridFS(db, bucket_name); fs_cache.put(fs_cache_key, fs); } GridFSDBFile db_file_old = fs.findOne(file_name); if (db_file_old == null) { error(res, SC_NOT_FOUND, Status.get("file doe not exists, use PUT")); return; } String ct = req.getContentType(); GridFSInputFile db_file = fs.createFile(file_name); if (ct != null) db_file.setContentType(ct); OutputStream os = db_file.getOutputStream(); final int len = 4096; byte data[] = new byte[len]; int n = 0, bytesno = 0; while ((n = is.read(data, 0, len)) > 0) { os.write(data, 0, n); bytesno += n; } os.close(); if (is != null) is.close(); out_json(req, Status.OK); }
From source file:com.andreig.jetty.GridfsServlet.java
License:GNU General Public License
@Override protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.fine("doPut()"); if (!can_write(req)) { res.sendError(SC_UNAUTHORIZED);//from w w w . ja va2 s . com return; } InputStream tmp = req.getInputStream(); InputStream is = new BufferedInputStream(tmp); String db_name = req.getParameter("dbname"); String bucket_name = req.getParameter("bucketname"); if (db_name == null || bucket_name == null) { String names[] = req2mongonames(req); if (names != null) { db_name = names[0]; bucket_name = names[1]; } if (db_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } } if (bucket_name == null) bucket_name = "fs"; String file_name = req.getParameter("filename"); if (file_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } DB db = mongo.getDB(db_name); String fs_cache_key = db_name + bucket_name; GridFS fs = fs_cache.get(fs_cache_key); if (fs == null) { fs = new GridFS(db, bucket_name); fs_cache.put(fs_cache_key, fs); } GridFSDBFile db_file_old = fs.findOne(file_name); if (db_file_old != null) { error(res, SC_BAD_REQUEST, Status.get("file already exists, use POST")); return; } String ct = req.getContentType(); GridFSInputFile db_file = fs.createFile(file_name); if (ct != null) db_file.setContentType(ct); OutputStream os = db_file.getOutputStream(); final int len = 4096; byte data[] = new byte[len]; @SuppressWarnings("unused") int n = 0, bytesno = 0; while ((n = is.read(data, 0, len)) > 0) { os.write(data, 0, n); bytesno += n; } os.flush(); os.close(); if (is != null) is.close(); out_json(req, Status.OK); }
From source file:com.bluedragon.mongo.gridfs.Add.java
License:Open Source License
public cfData execute(cfSession _session, cfArgStructData argStruct) throws cfmRunTimeException { // Get the necessary Mongo references DB db = getDB(_session, argStruct);/* ww w .j a v a2 s.c o m*/ GridFS gridfs = getGridFS(_session, argStruct, db); GridFSInputFile fsInputFile = null; // Get the file information String filename = getNamedStringParam(argStruct, "filename", null); if (filename == null) throwException(_session, "please specify a filename"); try { cfData ftmp = getNamedParam(argStruct, "file", null); if (ftmp.getDataType() == cfData.CFBINARYDATA) { fsInputFile = gridfs.createFile(((cfBinaryData) ftmp).getByteArray()); } else { // The 'file' parameter is a string, which means it is a path to a file File inputFile = new File(ftmp.getString()); if (!inputFile.exists()) throwException(_session, "File:" + inputFile + " does not exist"); if (!inputFile.isFile()) throwException(_session, "File:" + inputFile + " is not a valid file"); fsInputFile = gridfs.createFile(inputFile); } } catch (IOException e) { throwException(_session, e.getMessage()); } fsInputFile.setFilename(filename); String contenttype = getNamedStringParam(argStruct, "contenttype", null); if (contenttype != null) fsInputFile.setContentType(contenttype); String _id = getNamedStringParam(argStruct, "_id", null); if (_id != null) fsInputFile.setId(_id); // Get and set the metadata cfData mTmp = getNamedParam(argStruct, "metadata", null); if (mTmp != null) fsInputFile.setMetaData(getDBObject(mTmp)); // Save the Object try { fsInputFile.save(); return new cfStringData(fsInputFile.getId().toString()); } catch (MongoException me) { throwException(_session, me.getMessage()); return null; } }
From source file:com.buzz.buzzdata.MongoBuzz.java
@Override public void Insert(String userid, String header, String content, Double lat, Double lng, String tags, String[] files) {//from w ww . ja v a2s. co m BasicDBObject document = new BasicDBObject(); document.put("userid", userid); document.put("header", header); document.put("content", content); document.put("tags", tags.split(",")); document.put("created", (new Date())); document.put("modified", (new Date())); BasicDBObject lng_obj = new BasicDBObject(); lng_obj.put("lng", lng); BasicDBObject lat_obj = new BasicDBObject(); lat_obj.put("lat", lat); document.put("loc", (new Double[] { lng, lat })); document.put("FilesCount", files.length); DBCollection coll = mongoDB.getCollection("BuzzInfo"); coll.insert(document); ObjectId buzz_id = (ObjectId) document.get("_id"); int i = 0; for (String file : files) { try { GridFS gridFS = new GridFS(mongoDB); InputStream file_stream = getFTPInputStream(file); String caption_filename = FilenameUtils.removeExtension(file) + "_caption.txt"; InputStream caption_stream = getFTPInputStream(caption_filename); StringWriter writer = new StringWriter(); Charset par = null; IOUtils.copy(caption_stream, writer, par); String caption = writer.toString(); GridFSInputFile in = gridFS.createFile(file_stream); in.setFilename(file); in.put("BuzzID", buzz_id); in.put("Caption", caption); in.put("PicNum", i); in.save(); } catch (IOException ex) { Logger.getLogger(MongoBuzz.class.getName()).log(Level.SEVERE, null, ex); } i++; } }
From source file:com.card.loop.xyz.dao.LearningElementDAO.java
public boolean addFile(LearningElement le) throws UnknownHostException, IOException { File file = new File(AppConfig.USER_VARIABLE + le.getFilePath() + le.getFilename()); Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port); DB db = mongo.getDB(AppConfig.DATABASE_LOOP); GridFS gf = new GridFS(db, "le.meta"); GridFSInputFile gfsFile = gf.createFile(file); gfsFile.setFilename(le.getFilename()); gfsFile.setContentType(le.getContentType()); gfsFile.put("_class", "com.card.loop.xyz.model.LearningElement"); gfsFile.put("title", le.getTitle()); gfsFile.put("filePath", le.getFilePath()); gfsFile.put("subject", le.getSubject()); gfsFile.put("description", le.getDescription()); gfsFile.put("downloads", le.getDownloads()); gfsFile.put("rating", le.getRating()); gfsFile.put("comments", le.getComments()); gfsFile.put("uploadedBy", le.getUploadedBy()); gfsFile.put("status", le.getStatus()); gfsFile.put("rev", le.getRev()); gfsFile.put("type", le.getType()); gfsFile.save();/* w w w . jav a 2s .co m*/ // Let's store our document to MongoDB /* System.out.println("SEARCH: " + search(gfsFile.getMD5(), "le.meta")); if(search(gfsFile.getMD5(), "le.meta") > 1){ deleteLE(le.getFileName(),"le.meta"); }*/ // // collection.insert(info, WriteConcern.SAFE); return true; }
From source file:com.card.loop.xyz.dao.LearningElementDAO.java
public boolean addFile(LearningElement le, File file) throws UnknownHostException, IOException { Mongo mongo = new Mongo(AppConfig.mongodb_host, AppConfig.mongodb_port); DB db = mongo.getDB(AppConfig.DATABASE_LOOP); GridFS gf = new GridFS(db, "le.meta"); GridFSInputFile gfsFile = gf.createFile(file); gfsFile.setFilename(le.getFilename()); gfsFile.setContentType(le.getContentType()); gfsFile.put("_class", "com.card.loop.xyz.model.LearningElement"); gfsFile.put("title", le.getTitle()); gfsFile.put("filePath", le.getFilePath()); gfsFile.put("subject", le.getSubject()); gfsFile.put("description", le.getDescription()); gfsFile.put("downloads", le.getDownloads()); gfsFile.put("rating", le.getRating()); gfsFile.put("comments", le.getComments()); gfsFile.put("uploadedBy", le.getUploadedBy()); gfsFile.put("status", le.getStatus()); gfsFile.put("rev", le.getRev()); gfsFile.put("type", le.getType()); gfsFile.save();/*from w w w.j a va2 s. c om*/ // Let's store our document to MongoDB /* System.out.println("SEARCH: " + search(gfsFile.getMD5(), "le.meta")); if(search(gfsFile.getMD5(), "le.meta") > 1){ deleteLE(le.getFileName(),"le.meta"); }*/ // // collection.insert(info, WriteConcern.SAFE); return true; }
From source file:com.cognifide.aet.vs.artifacts.ArtifactsDAOMongoDBImpl.java
License:Apache License
@Override public String saveArtifact(DBKey dbKey, InputStream data, String contentType) { String resultObjectId = null; GridFS gfs = getGridFS(dbKey); if (gfs != null) { GridFSInputFile file = gfs.createFile(data); if (file != null) { file.setContentType(contentType); file.save();/* w w w . j a v a 2 s. co m*/ resultObjectId = file.getId().toString(); } } return resultObjectId; }
From source file:com.cyslab.craftvm.rest.mongo.GridfsServlet.java
License:GNU General Public License
@Override protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { log.trace("doPost()"); if (!can_write(req)) { res.sendError(SC_UNAUTHORIZED);/* w w w . ja v a 2 s. c o m*/ return; } InputStream tmp = req.getInputStream(); InputStream is = new BufferedInputStream(tmp); String db_name = req.getParameter("dbname"); String bucket_name = req.getParameter("bucketname"); if (db_name == null || bucket_name == null) { String names[] = req2mongonames(req); if (names != null) { db_name = names[0]; bucket_name = names[1]; } if (db_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } } if (bucket_name == null) bucket_name = "fs"; String file_name = req.getParameter("filename"); if (file_name == null) { error(res, SC_BAD_REQUEST, Status.get("param name missing")); return; } DB db = mongo.getDB(db_name); String fs_cache_key = db_name + bucket_name; GridFS fs = fs_cache.get(fs_cache_key); if (fs == null) { fs = new GridFS(db, bucket_name); fs_cache.put(fs_cache_key, fs); } GridFSDBFile db_file_old = fs.findOne(file_name); if (db_file_old == null) { error(res, SC_NOT_FOUND, Status.get("file doe not exists, use PUT")); return; } String ct = req.getContentType(); GridFSInputFile db_file = fs.createFile(file_name); if (ct != null) db_file.setContentType(ct); OutputStream os = db_file.getOutputStream(); final int len = 4096; byte data[] = new byte[len]; int n = 0, bytesno = 0; while ((n = is.read(data, 0, len)) > 0) { os.write(data, 0, n); bytesno += n; } os.close(); if (is != null) is.close(); out_json(req, Status.OK); }