List of usage examples for com.mongodb.gridfs GridFSInputFile setContentType
public void setContentType(final String contentType)
From source file:UnitTest3.java
License:Open Source License
public static int testgridfs() { long time1;/*from ww w . j ava2 s. co 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:cfel.service.DatabaseServive.java
License:Open Source License
public OutputStream getFileOutputStream(String id, String contentType) { if (mFS != null) { try {// www. j a v a2 s. c o m deleteFile(id); GridFSInputFile dbFile = mFS.createFile(id); if (contentType != null) { dbFile.setContentType(contentType); } return dbFile.getOutputStream(); } catch (Exception e) { e.printStackTrace(); } } return null; }
From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java
License:Apache License
protected void writeFile(InputStream ins, MFile mf, String tableName) { DB db = options.getCollection(TABLE_TEMP_KEY).getDB(); db.requestStart();/*from ww w . ja va 2 s . c o m*/ GridFSInputFile gfsInput; gfsInput = new GridFS(db, tableName).createFile(ins); DBCollection col = db.getCollection(tableName + ".files"); col.setWriteConcern(WriteConcern.SAFE); gfsInput.setFilename(mf.getFilename()); gfsInput.put(FIELD_STORAGE_KEY, mf.getStorageKey()); gfsInput.put(FIELD_DOC_ID, mf.getDocid()); gfsInput.put(FILED_VERSION_ID, mf.getVid()); gfsInput.put(FIELD_APP_ID, mf.getAppid()); gfsInput.put(FILED_IS_PUB, getIsPubStatus(mf.getIsPub())); gfsInput.setContentType(mf.getContentType()); gfsInput.save(); db.requestDone(); }
From source file:cn.vlabs.clb.server.storage.mongo.MongoStorageService.java
License:Apache License
protected void writeTrivialFile(InputStream ins, MTrivialFile mtf, String tableName) { DB db = options.getCollection(TABLE_TEMP_KEY).getDB(); db.requestStart();// w ww. j a v a2 s . c om GridFSInputFile gfsInput; DBCollection col = db.getCollection(tableName + ".files"); col.setWriteConcern(WriteConcern.SAFE); gfsInput = new GridFS(db, tableName).createFile(ins); gfsInput.setFilename(mtf.getFileName()); gfsInput.put(FIELD_STORAGE_KEY, mtf.getStorageKey()); gfsInput.put(FIELD_SPACE_NAME, mtf.getSpaceName()); gfsInput.setContentType(mtf.getContentType()); gfsInput.save(); db.requestDone(); }
From source file:com.andreig.jetty.GridfsServlet.java
License:GNU General Public License
@SuppressWarnings("unused") @Override/*from www . 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);/* w ww . j ava 2s . 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);/*from w w w .j a v a2 s . com*/ 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.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();//from ww w . ja v a 2 s . c o 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();/* w ww. j ava 2 s . c o 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.cloudbees.demo.beesshop.service.MongoDbFileStorageService.java
License:Apache License
@Nonnull public String storeFile(InputStream in, String contentType) { String extension = defaultFileExtensionByContentType.get(contentType); String fileName = Joiner.on(".").skipNulls().join(Math.abs(random.nextLong()), extension); GridFSInputFile file = gridFS.createFile(in, fileName); file.setContentType(contentType); file.save();/*from w w w. java2s . co m*/ return "gridfs://" + gridFS.getBucketName() + "/" + file.getId().toString(); }