Example usage for com.mongodb.gridfs GridFS getFileList

List of usage examples for com.mongodb.gridfs GridFS getFileList

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS getFileList.

Prototype

public DBCursor getFileList(final DBObject query) 

Source Link

Document

Gets a filtered list of files stored in this gridfs, sorted by filename.

Usage

From source file:de.iew.imageread.Main.java

License:Apache License

public void run() {
    try {/* www. java2  s  .  c o  m*/
        this.imageOutputDir = System.getProperty("java.io.tmpdir");

        File outputBase = new File(this.imageOutputDir);
        testAndCreateDirectory(outputBase);

        MongoClient mongoClient = new MongoClient(this.mongohost, this.mongoport);

        DB db = mongoClient.getDB(this.mongodb);

        GridFS gridFS = new GridFS(db);

        DBCursor cursor = gridFS.getFileList(this.queryFromOptions()).sort(new BasicDBObject("uploadDate", -1));

        int imageNumber = 0;
        while (cursor.hasNext()) {
            DBObject fileObject = cursor.next();

            GridFSDBFile file = gridFS.find((ObjectId) fileObject.get("_id"));
            printGridFSDBFile(file);

            if (writeImageFile(outputBase, imageNumber, file)) {
                imageNumber++;
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}