Example usage for com.mongodb.gridfs GridFSInputFile save

List of usage examples for com.mongodb.gridfs GridFSInputFile save

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFSInputFile save.

Prototype

@Override
public void save() 

Source Link

Document

Calls GridFSInputFile#save(long) with the existing chunk size.

Usage

From source file:UnitTest3.java

License:Open Source License

public static int testgridfs() {

    long time1;/* w w w .  ja  va2s . 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 .j  a va 2s  . co  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:cfel.test.InitAlbum.java

License:Open Source License

private static void addImages(File dir) {
    dir.listFiles(new FileFilter() {
        @Override/* ww  w .ja v  a2  s  . c  o m*/
        public boolean accept(File file) {
            if (file.isDirectory()) {
                addImages(file);
            } else {
                String fileName = file.getName().toLowerCase();
                if (fileName.endsWith(".jpg") || fileName.endsWith(".png")) {
                    String path = "img/" + file.toURI().getPath().substring(IMAGE_DIR_PATH.length());
                    System.out.println(path);
                    try {
                        GridFSInputFile gfFile = fs.createFile(new FileInputStream(file), path, true);
                        gfFile.save();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return false;
        }
    });
}

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  w w  w  .ja  v  a2s . co 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();/*from   w  w  w.ja  v  a  2 s  .  com*/
    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.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. jav  a 2 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.bugull.mongo.fs.BuguFS.java

License:Apache License

public void save(File file, String filename, Map<String, Object> attributes) {
    GridFSInputFile f = null;
    try {//from   w  w w  .  ja v a2 s .c om
        f = fs.createFile(file);
    } catch (IOException ex) {
        logger.error("Can not create GridFSInputFile", ex);
    }
    f.setChunkSize(chunkSize);
    f.setFilename(filename);
    setAttributes(f, attributes);
    f.save();
}

From source file:com.bugull.mongo.fs.BuguFS.java

License:Apache License

public void save(InputStream is, String filename, Map<String, Object> attributes) {
    GridFSInputFile f = fs.createFile(is);
    f.setChunkSize(chunkSize);/*from   ww w.j  a va 2s.  c o m*/
    f.setFilename(filename);
    setAttributes(f, attributes);
    f.save();
}

From source file:com.bugull.mongo.fs.BuguFS.java

License:Apache License

public void save(byte[] data, String filename, Map<String, Object> attributes) {
    GridFSInputFile f = fs.createFile(data);
    f.setChunkSize(chunkSize);/*from  w w  w  .  jav a 2s.  co m*/
    f.setFilename(filename);
    setAttributes(f, attributes);
    f.save();
}

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 ww w .j av a 2 s. c  o  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++;
    }
}