Save the file to GridFS with the given path

Given the input stream, the line for the file name and the line for the path, how to save the file to GridFS using Java? Right now I have a save without a path:

public ObjectId saveFile(InputStream inputStream, String filename, String folder) { 
   GridFSInputFile gInputFile = gridfs.createFile(inputStream, filename);
   gInputFile.save();
   return ObjectId.massageToObjectId( gInputFile.getId() );
}
+3
source share
1 answer

GridFS does not store files as a structure, such as a file system hierarchy. Thus, there is no path associated with saved files. But you can add the 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 a path attribute.

+2
source

All Articles