List of usage examples for com.mongodb.gridfs GridFSDBFile writeTo
public long writeTo(final OutputStream out) throws IOException
From source file:rapture.blob.mongodb.GridFSBlobHandler.java
License:Open Source License
protected GridFSInputFile updateExisting(CallingContext context, String docPath, InputStream newContent, GridFS gridFS, GridFSDBFile existing) throws IOException { GridFSInputFile file;//from w ww . jav a 2s.co m String lockKey = createLockKey(gridFS, docPath); LockHandle lockHandle = grabLock(context, lockKey); try { File tempFile = File.createTempFile("rapture", "blob"); existing.writeTo(tempFile); FileInputStream tempIn = FileUtils.openInputStream(tempFile); SequenceInputStream sequence = new SequenceInputStream(tempIn, newContent); try { gridFS.remove(docPath); file = createNewFile(docPath, sequence); if (!tempFile.delete()) { log.warn(String.format("Unable to delete temp file created while appending docPath %s, at %s", docPath, tempFile.getAbsolutePath())); } } finally { try { sequence.close(); } catch (IOException e) { log.error( String.format("Error closing sequence input stream: %s", ExceptionToString.format(e))); } try { tempIn.close(); } catch (IOException e) { log.error( String.format("Error closing sequence input stream: %s", ExceptionToString.format(e))); } } } finally { releaseLock(context, lockKey, lockHandle); } return file; }
From source file:rmi_video.VideoServer.java
@Override public VideoData getVideo(String id) throws RemoteException { try {// w w w . j a v a 2 s. c o m //Cria conexao com MongoDB MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("VideoDatabase"); //Recupera o video atraves do ID GridFS gfsVideo = new GridFS(db, "video"); BasicDBObject whereQuery = new BasicDBObject(); whereQuery.put("videoId", id); GridFSDBFile videoForOutput = gfsVideo.findOne(whereQuery); String filename = videoForOutput.getFilename(); try (FileOutputStream fos = new FileOutputStream("/Users/philcr/Documents/" + filename)) { videoForOutput.writeTo(fos); } Path path = Paths.get("/Users/philcr/Documents/" + filename); byte[] data = Files.readAllBytes(path); File videoFile = new File("/Users/philcr/Documents/" + filename); //Exclui o arquivo local boolean deletedFlag = videoFile.delete(); if (!deletedFlag) { System.err.println("Video could not be deleted!"); } mongoClient.close(); return new VideoData(data, filename, id); } catch (UnknownHostException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (FileNotFoundException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } catch (IOException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return null; } }