發表文章

目前顯示的是有「MongDB」標籤的文章

Solution for MongodbException: can't find a chunk! when using GridFS

public void insertFileByGridFS(byte[] data, ObjectId id, String filename) throws Exception{ GridFS gfs = new GridFS(database, "pdf"); GridFSInputFile gfsFile = gfs.createFile(data.clone()); gfsFile.setContentType("application/pdf"); //GridFS will set the parent file id for the chunk id by default. //if set the id by setId(new ObjectId()), the file_id of the chunk will be null gfsFile.setId(id); gfsFile.setFilename(filename); gfsFile.save(); } Please don't explicitly call GridFSInputFile.setId(), otherwise the file_id field of chunk will be null, you can't retrieve the file by GridFSDBFile.findOne(filename) (file name is store in the files collection and link to chunk collection by the file_id)