List of usage examples for com.mongodb.gridfs GridFSInputFile put
@Override public Object put(final String key, final Object v)
From source file:org.rhq.enterprise.server.plugins.drift.mongodb.dao.FileDAO.java
License:Open Source License
public void save(File file) throws IOException { GridFSInputFile inputFile = gridFS.createFile(new BufferedInputStream(new FileInputStream(file))); inputFile.put("_id", file.getName()); inputFile.save();//from w w w.j a v a 2 s.co m }
From source file:rmi_video.VideoServer.java
@Override public boolean addVideo(VideoData d) throws RemoteException { try {/*from www . ja v a 2 s .c o m*/ //Conexao com mongoDB MongoClient mongoClient = new MongoClient("localhost", 27017); DB db = mongoClient.getDB("VideoDatabase"); try (FileOutputStream fos = new FileOutputStream("/Users/philcr/Documents/" + d.getFileName())) { fos.write(d.getData()); } File videoFile = new File("/Users/philcr/Documents/" + d.getFileName()); GridFS gfsVideo = new GridFS(db, "video"); //Cria e salva o arquivo no DB pelo GridFS GridFSInputFile gfsFile = gfsVideo.createFile(videoFile); gfsFile.setId(new ObjectId()); //Utiliza a criao de ID do mongo gfsFile.put("videoId", d.getId()); //Utiliza nosso metodo de ID gfsFile.setFilename(d.getFileName()); gfsFile.save(); //Exclui o arquivo local boolean deletedFlag = videoFile.delete(); if (!deletedFlag) { System.err.println("File could not be deleted!"); } mongoClient.close(); return true; } catch (UnknownHostException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (FileNotFoundException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } catch (IOException ex) { Logger.getLogger(VideoServer.class.getName()).log(Level.SEVERE, null, ex); return false; } }
From source file:tango.mongo.ImageManager.java
License:Open Source License
public synchronized boolean saveInputImage(ObjectId xpId, ObjectId field_id, int fileRank, ImageHandler img, boolean flushImage) { if (img == null) return false; //IJ.log("file: "+img.getTitle()+" size:"+img.getSizeInMb()+ " available memory:"+Core.getAvailableMemory()+ " please free memory"); double scaleZ = img.getScaleZ(); String unit = img.getUnit();/*from w ww . j a v a 2s.co m*/ String title = img.getTitle(); MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); try { byte[] data = img.getBinaryData(); if (data == null) { IJ.log("couldn't save image:" + title); return false; } if (flushImage) img.flush(); GridFSInputFile gfi = this.gfsField.get(xpId).createFile(data); data = null; gfi.setFilename(title); gfi.put("field_id", field_id); gfi.put("fileRank", fileRank); gfi.put("pixelDepth", scaleZ); gfi.put("unit", unit); removeInputImage(xpId, field_id, fileRank); gfi.save(); gfi.getOutputStream().close(); return true; } catch (Exception e) { exceptionPrinter.print(e, "Error while saving image: " + title, true); } catch (OutOfMemoryError e) { int MEGABYTE = (1024 * 1024); MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); long maxMemory = heapUsage.getMax() / MEGABYTE; long usedMemory = heapUsage.getUsed() / MEGABYTE; IJ.log("Error while saving image:" + title + " Out of memory. Memory Use :" + usedMemory + "M/" + maxMemory + "M"); } return false; }
From source file:tango.mongo.ImageManager.java
License:Open Source License
public synchronized void saveFieldThumbnail(ObjectId field_id, ImageHandler img, int sizeX, int sizeY) { GridFSInputFile gfi = this.gfsFieldThumbnail.createFile(img.getThumbNail(sizeX, sizeY)); BasicDBObject query = new BasicDBObject("field_id", field_id); gfsFieldThumbnail.remove(query);// w w w . j av a 2s . co m gfi.put("field_id", field_id); gfi.save(); try { gfi.getOutputStream().close(); } catch (Exception e) { exceptionPrinter.print(e, "", Core.GUIMode); } }
From source file:tango.mongo.ImageManager.java
License:Open Source License
public synchronized void saveNucleusImage(ObjectId xpId, ObjectId nucleus_id, int fileIdx, int fileType, ImageHandler img) {// w w w. j a va 2 s .co m if (img == null) return; removeNucleusImage(xpId, nucleus_id, fileIdx, fileType); try { GridFSInputFile gfi = this.gfsNucleus.get(xpId).createFile(img.getBinaryData()); gfi.setFilename(img.getImagePlus().getShortTitle()); gfi.put("nucleus_id", nucleus_id); gfi.put("fileIdx", fileIdx); gfi.put("fileType", fileType); gfi.put("pixelDepth", img.getScaleZ()); gfi.put("unit", img.getUnit()); gfi.save(); if (gfi != null) gfi.getOutputStream().close(); } catch (Exception e) { exceptionPrinter.print(e, "Error while saving image:" + img.getTitle(), Core.GUIMode); } }
From source file:tango.mongo.ImageManager.java
License:Open Source License
public synchronized void saveChannelImageThumbnail(ObjectId nucleus_id, int fileIdx, ImageHandler img, int sizeX, int sizeY, ImageInt mask) { GridFSInputFile gfi = this.gfsNucleusThumbnail.createFile(img.getThumbNail(sizeX, sizeY, mask)); BasicDBObject query = new BasicDBObject("nucleus_id", nucleus_id).append("fileRank", fileIdx); gfsNucleusThumbnail.remove(query);//from w ww . j a v a2s . co m gfi.put("nucleus_id", nucleus_id); gfi.put("fileRank", fileIdx); gfi.save(); try { gfi.getOutputStream().close(); } catch (Exception e) { exceptionPrinter.print(e, "", Core.GUIMode); } }
From source file:tango.mongo.ImageManager.java
License:Open Source License
private void transferFiles(String queryField, ObjectId queryValue, GridFS gfsSource, GridFS gfsDestination) { BasicDBObject query = new BasicDBObject(queryField, queryValue); List<GridFSDBFile> files = gfsSource.find(query); // FIXME charge tout en mmoire? for (GridFSDBFile file : files) { GridFSInputFile gfi = gfsDestination.createFile(file.getInputStream(), file.getFilename()); gfi.put(queryField, queryValue); gfi.put("fileIdx", file.get("fileIdx")); gfi.put("fileType", file.get("fileType")); gfi.put("pixelDepth", file.get("pixelDepth")); gfi.put("unit", file.get("unit")); gfi.save();/*from www.j av a 2 s.c o m*/ if (gfi != null) try { gfi.getOutputStream().close(); } catch (IOException ex) { Logger.getLogger(ImageManager.class.getName()).log(Level.SEVERE, null, ex); } } gfsSource.remove(query); }
From source file:tango.mongo.MongoConnector.java
License:Open Source License
public synchronized boolean saveInputImage(ObjectId field_id, int fileRank, ImageHandler img, boolean flushImage) { if (img == null) { return false; }//from w w w . j a v a 2 s. c o m //IJ.log("file: "+img.getTitle()+" size:"+img.getSizeInMb()+ " available memory:"+Core.getAvailableMemory()+ " please free memory"); double scaleZ = img.getScaleZ(); String unit = img.getUnit(); String title = img.getTitle(); MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean(); try { byte[] data = img.getBinaryData(); if (data == null) { IJ.log("couldn't save image:" + title); return false; } if (flushImage) { img.flush(); } GridFSInputFile gfi = this.gfsField.createFile(data); data = null; gfi.setFilename(title); gfi.put("field_id", field_id); gfi.put("fileRank", fileRank); gfi.put("pixelDepth", scaleZ); gfi.put("unit", unit); removeInputImage(field_id, fileRank); gfi.save(); gfi.getOutputStream().close(); return true; } catch (Exception e) { exceptionPrinter.print(e, "Error while saving image: " + title, true); } catch (OutOfMemoryError e) { int MEGABYTE = (1024 * 1024); MemoryUsage heapUsage = memoryBean.getHeapMemoryUsage(); long maxMemory = heapUsage.getMax() / MEGABYTE; long usedMemory = heapUsage.getUsed() / MEGABYTE; IJ.log("Error while saving image:" + title + " Out of memory. Memory Use :" + usedMemory + "M/" + maxMemory + "M"); } return false; }
From source file:tango.mongo.MongoConnector.java
License:Open Source License
public synchronized void saveFieldThumbnail(ObjectId field_id, int fileIdx, byte[] thumbnail) { GridFSInputFile gfi = this.gfsFieldThumbnail.createFile(thumbnail); BasicDBObject query = new BasicDBObject("field_id", field_id).append("fileRank", fileIdx); gfsFieldThumbnail.remove(query);/* w w w .jav a 2 s. co m*/ gfi.put("field_id", field_id); gfi.put("fileRank", fileIdx); gfi.save(); try { gfi.getOutputStream().close(); } catch (Exception e) { exceptionPrinter.print(e, "", Core.GUIMode); } }
From source file:tango.mongo.MongoConnector.java
License:Open Source License
public synchronized void saveNucleusImage(ObjectId nucleus_id, int fileIdx, int fileType, ImageHandler img) { removeNucleusImage(nucleus_id, fileIdx, fileType); if (img == null) { System.out.println("set nucleus image null"); return;//from w w w . ja v a2 s . c om } try { GridFSInputFile gfi = this.gfsNucleus.createFile(img.getBinaryData()); gfi.setFilename(img.getImagePlus().getShortTitle()); gfi.put("nucleus_id", nucleus_id); gfi.put("fileIdx", fileIdx); gfi.put("fileType", fileType); gfi.put("pixelDepth", img.getScaleZ()); gfi.put("unit", img.getUnit()); gfi.put("offsetX", img.offsetX); gfi.put("offsetY", img.offsetY); gfi.put("offsetZ", img.offsetZ); gfi.save(); if (gfi != null) { gfi.getOutputStream().close(); } } catch (Exception e) { exceptionPrinter.print(e, "Error while saving image:" + img.getTitle(), Core.GUIMode); } }