MongoDB 取出文件遇到异常


  • 註冊用戶

    前些天终于把文件塞进mongoDB里边了,然后开始考虑拉取。

    public static void test1() {
        GridFS gfs=new GridFS(new DB(new MongoClient(),"listentome"));
        DBCursor cur=gfs.getFileList();
        int num=0;
        while(cur.hasNext())
        {
            GridFSDBFile dbf=(GridFSDBFile)cur.next();
            try {
                dbf.writeTo("E:/"+(num++)+".mp3");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    然而异常来了

    Exception in thread "main" java.lang.IllegalStateException: No GridFS instance defined!
    at com.mongodb.gridfs.GridFSDBFile.getChunk(GridFSDBFile.java:95)
    at com.mongodb.gridfs.GridFSDBFile.writeTo(GridFSDBFile.java:88)
    at com.mongodb.gridfs.GridFSDBFile.writeTo(GridFSDBFile.java:70)
    at com.mongodb.gridfs.GridFSDBFile.writeTo(GridFSDBFile.java:56)
    at com.listentome.DB.Operations.IDB.test1(IDB.java:112)
    at com.listentome.DB.Operations.IDB.main(IDB.java:101)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)


  • Lv 1

    @oughl0007

    你的版本和我不一樣... 我是

    1. mongoClient.getDatabase()

    2. GridFSBuckets.create(database)

    3. gridBucket.uploadFromStream(fileName, inputStream, uploadOptions)


登录后回复
 

与 萌阔论坛 的连接断开,我们正在尝试重连,请耐心等待