java 实现mongodb中gridFS文件存储


//保存文件

@Override

public void saveFileToMongo(SpecialPage page,String path) {
try {
File file = new File(path+page.getZipName());
GridFS gridfs = getGridFS();
GridFSInputFile gfile = gridfs.createFile(file);
gfile.setFilename(page.getZipName());
gfile.save();
} catch (Exception e) {
e.printStackTrace();
}

}


private GridFS getGridFS(){
DB db = this.mongoTemplate.getDb();
GridFS gridfs = new GridFS(db);
return gridfs;
}


//查询文件

GridFS gridfs = getGridFS();
GridFSDBFile gfs = gridfs.findOne(page.getZipName());
gfs.writeTo(file);

你可能感兴趣的:(JAVA)