无法使用java驱动程序从gridFs检索文件(但可以使用python)

时间:2013-10-06 19:25:12

标签: java mongodb gridfs mongodb-java

请注意;我们现在已经完全重写了python中的后端,所以尽管知道我们做错了什么仍然很好,但它不再那么重要了。

我的数据库中有一个已知文件,其中包含以下objectID“5251ad0d56c02c34fbad2a3d”

我知道它就在那里,因为我可以使用mongo cli客户端和pymongo来检查它:

>>> import pymongo, gridfs, bson
>>> client = pymongo.MongoClient()
>>> db = client['playtest']
>>> fs = gridfs.GridFS(db, 'attachments')
>>> gridout = fs.get(bson.objectid.ObjectId("5251ad0d56c02c34fbad2a3d"))
>>> gridout.filename
u'vasalis-03.png'
>>>

但是,当我尝试使用带有以下代码的java检索它时

import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import com.mongodb.MongoClient;
import org.bson.types.ObjectId;

import java.io.File;

public class RetrieveFileTest {
  public static void main(String[] args) throws Exception {
    MongoClient mongoClient = new MongoClient();
    DB db = mongoClient.getDB( "playtest" );
    GridFS gfs = new GridFS(db, "attachments");

    System.out.println("getting file: " + args[0]);
    ObjectId fileId = new ObjectId(args[0]);
    GridFSDBFile gfsFile = gfs.findOne(fileId);
    File outFile = new File(gfsFile.getFilename());
    gfsFile.writeTo(outFile);
  }
}

我收到此错误消息:

getting file: 5251ad0d56c02c34fbad2a3d
Oct 06, 2013 7:01:45 PM com.mongodb.DBPortPool gotError
WARNING: emptying DBPortPool to /127.0.0.1:27017 b/c of error
java.lang.ClassCastException: com.mongodb.BasicDBList cannot be cast to java.lang.String
    at com.mongodb.gridfs.GridFSFile.put(GridFSFile.java:192)
    at org.bson.BasicBSONCallback.objectStart(BasicBSONCallback.java:69)
    at com.mongodb.DefaultDBCallback.objectStart(DefaultDBCallback.java:64)
    at org.bson.BasicBSONCallback.arrayStart(BasicBSONCallback.java:88)
    at org.bson.BasicBSONDecoder.decodeElement(BasicBSONDecoder.java:195)
    at org.bson.BasicBSONDecoder._decode(BasicBSONDecoder.java:79)
    at org.bson.BasicBSONDecoder.decode(BasicBSONDecoder.java:57)
    at com.mongodb.DefaultDBDecoder.decode(DefaultDBDecoder.java:61)
    at com.mongodb.Response.<init>(Response.java:83)
    at com.mongodb.DBPort.go(DBPort.java:142)
    at com.mongodb.DBPort.call(DBPort.java:92)
    at com.mongodb.DBTCPConnector.innerCall(DBTCPConnector.java:244)
    at com.mongodb.DBTCPConnector.call(DBTCPConnector.java:216)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:288)
    at com.mongodb.DBApiLayer$MyCollection.__find(DBApiLayer.java:273)
    at com.mongodb.DBCollection.findOne(DBCollection.java:728)
    at com.mongodb.DBCollection.findOne(DBCollection.java:670)
    at com.mongodb.gridfs.GridFS.findOne(GridFS.java:191)
    at com.mongodb.gridfs.GridFS.findOne(GridFS.java:173)
    at nl.lntng.storm_project.RetrieveFileTest.main(RetrieveFileTest.java:30)
    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 org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
    at java.lang.Thread.run(Thread.java:724)

并且不知道如何处理它。

0 个答案:

没有答案