import com.mongodb.DB;//加载MongoDB的java驱动
import com.mongodb.MongoException;
import com.mongodb.gridfs.GridFSFile;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSInputFile;
import com.mongodb.*;
public class connectMongoDB {
public static void main(String[] args) throws MongoException, IOException {
try{
long start = new Date().getTime();
Mongo m = new Mongo("192.168.10.221", 27017);
DB db = m.getDB("test");
//db.authenticate("test", "123".toCharArray());
File f = new File("D:\\2010.pdf");
GridFS myFS = new GridFS(db);
GridFSInputFile inputFile = myFS.createFile(f);
inputFile.save();
long endTime = new Date().getTime();
System.out.println(endTime-start);
System.out.println((endTime - start) / 10000000);
}
catch(Exception e){
e.printStackTrace();
}
}
}
com.mongodb.MongoException: Index with name: files_id_1_n_1 already exists with different options
at com.mongodb.CommandResult.getException(CommandResult.java:82)
at com.mongodb.CommandResult.throwOnError(CommandResult.java:116)
at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:126)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:148)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:132)
at com.mongodb.DBApiLayer$MyCollection.insert(DBApiLayer.java:255)
at com.mongodb.DBApiLayer$MyCollection.createIndex(DBApiLayer.java:347)
at com.mongodb.DBCollection.ensureIndex(DBCollection.java:437)
at com.mongodb.DBCollection.ensureIndex(DBCollection.java:385)
at com.mongodb.gridfs.GridFS.<init>(GridFS.java:79)
at com.mongodb.gridfs.GridFS.<init>(GridFS.java:58)
at connectMongoDB.main(connectMongoDB.java:22)
解决了,驱动的问题