Save file to GridFS with given path

gridfs, java

Solution

GridFS does not store files as a structure like file system hierarchy. So there isn't any path associated with stored files. But you can add a path field manually.

public ObjectId saveFile(InputStream inputStream, String filename, String folder) { 
    GridFSInputFile gInputFile = gridfs.createFile(inputStream, filename);
    gInputFile.put("path", folder);
    gInputFile.save();
    return ObjectId.massageToObjectId( gInputFile.getId() );
}

Now all files will have 'path' attribute.

Problem

Given an inputstream, a string for the filename, and a string for the path how do I store the file in GridFS using Java? Right now I have it saving without the path: ``` public ObjectId saveFile(InputStream inputStream, String filename, String folder) { GridFSInputFile gInputFile = gridfs.createFile(inputStream, filename); gInputFile.save(); return ObjectId.massageToObjectId( gInputFile.getId() ); } ```

Original source