Example usage for com.mongodb.gridfs GridFSInputFile setFilename

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

Introduction

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

Prototype

public void setFilename(final String filename) 

Source Link

Document

Sets the file name on the GridFS entry.

Usage

From source file:UnitTest3.java

License:Open Source License

public static int testgridfs() {

    long time1;/*  www  .  j a  va  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:calliope.db.MongoConnection.java

License:Open Source License

/**
 * Store an image in the database/*  w ww .  jav  a  2 s  . 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: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 www  .ja v  a2 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();//from  w w w  .j  a va  2 s  .  c  o m
    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);/*www. jav a 2 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.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  .jav  a 2s .co  m
        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  w  w w  .  j  a  va2s.co  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);/*w w  w .ja  v  a 2  s. 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  .  ja v  a2  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++;
    }
}

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 a2 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;
}