下面列出了com.mongodb.gridfs.GridFS#findOne ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
@Ignore
public void storeFile() throws IOException {
File file = new File("/home/darrenfu/IdeaProjects/pampas/pampas-grpc/df/open/grpc/hello/grpc-test-229014610914606914.jar");
GridFS gridFS = new GridFS(datastore.getDB());
GridFSInputFile gridFSInputFile = gridFS.createFile(file);
if (gridFS.findOne(file.getName()) == null) {
gridFSInputFile.setId(file.getName());
gridFSInputFile.setMetaData(new BasicDBObject("version", "1.1.2"));
gridFSInputFile.save();
}
GridFSDBFile fsdbFile = gridFS.findOne(file.getName());
File newfile = new File("/home/darrenfu/IdeaProjects/pampas/pampas-grpc/df/open/grpc/hello/grpc-test-229014610914606914.new.jar");
if (newfile.exists()) {
newfile.delete();
}
newfile.createNewFile();
newfile.setWritable(true);
fsdbFile.writeTo(newfile);
System.out.println("done : " + fsdbFile.getFilename());
}
@GET
@Path("/{id}")
public Response getAttachment(@PathParam("id") final String id) throws IOException {
final GridFS gridFS = new GridFS(this.mongoLegacyGrid);
final GridFSDBFile file = gridFS.findOne(id);
// log.info(file);
if (file == null) {
throw new WebApplicationException(404);
}
return Response.ok(org.apache.commons.io.IOUtils.toByteArray(file.getInputStream()), file.getContentType()).build();
}
private InputStream getMapFileByAppId(GridFS gridFS,String appIdentifier) throws MapFileNotExistsException{
InputStream fileMap=null;
GridFSDBFile gridFSFileMap = gridFS.findOne(appIdentifier);
if(gridFSFileMap==null){
throw new MapFileNotExistsException();
}
fileMap=gridFSFileMap.getInputStream();
return fileMap;
}
private void removeFile(String appIdentifier,GridFS gridFS){
GridFSDBFile gridFSFileRemoveFile = gridFS.findOne(appIdentifier);
if(gridFSFileRemoveFile!=null){
gridFS.remove(gridFSFileRemoveFile);
}
}
private void removeFile(String appIdentifier,GridFS gridFS){
GridFSDBFile gridFSFileRemoveFile = gridFS.findOne(appIdentifier);
if(gridFSFileRemoveFile!=null){
gridFS.remove(gridFSFileRemoveFile);
}
}
private void removeFile(String appIdentifier,GridFS gridFS){
GridFSDBFile gridFSFileRemoveFile = gridFS.findOne(appIdentifier);
if(gridFSFileRemoveFile!=null){
gridFS.remove(gridFSFileRemoveFile);
}
}