/** * Writes the file's data to a file on disk. * * @param filename the file name on disk * @return number of bytes written * @throws IOException if there are problems writing to the file */ public long writeTo(final String filename) throws IOException { return writeTo(new File(filename)); }
/** * Writes the file's data to a file on disk. * * @param file the File object * @return number of bytes written * @throws IOException if there are problems writing to the {@code file} */ public long writeTo(final File file) throws IOException { FileOutputStream out = null; try { out = new FileOutputStream(file); return writeTo(out); } finally { if (out != null) { out.close(); } } }
f.writeTo(f.getFilename()); return;
@Override public void writeToFile(File file) throws IOException { mongoFile.writeTo(file); }
GridFSDBFile file = ... ByteArrayOutputStream baos = new ByteArrayOutputStream(); file.writeTo(baos); byte[] bytes = baos.toByteArray();
/** * Writes the file's data to a file on disk. * * @param filename the file name on disk * @return number of bytes written * @throws IOException if there are problems writing to the file */ public long writeTo(final String filename) throws IOException { return writeTo(new File(filename)); }
/** * Writes the file's data to a file on disk. * * @param file the File object * @return number of bytes written * @throws IOException if there are problems writing to the {@code file} */ public long writeTo(final File file) throws IOException { FileOutputStream out = null; try { out = new FileOutputStream(file); return writeTo(out); } finally { if (out != null) { out.close(); } } }
@Override public void write(String contentFileId, OutputStream outputStream) throws IOException { getGridFSDBFile(contentFileId).writeTo(outputStream); }
public void writeFileTo(String key, HttpServletResponse resp) throws IOException { GridFSDBFile out = getFileService().findOne(new ObjectId(key)); if (out == null) { throw new FileNotFoundException(key); } resp.setContentType(out.getContentType()); // This might be a conflict out.writeTo(resp.getOutputStream());
GridFS gfs = new GridFS(db); ObjectId fileId = new ObjectId("your_object_id"); GridFSDBFile gfsFile = gfs.find(fileId); File outFile; RelativeLayout v = (RelativeLayout) findViewById(R.id.RelativeLayout); Context context = (Context) v.getContext(); outFile = File.createTempFile("xyz", null, context.getCacheDir()); gfsFile.writeTo(outFile); ImageView iv= (ImageView)findViewById(R.id.imageView1); InputStream is = new FileInputStream(outFile); iv.setImageBitmap(BitmapFactory.decodeStream(is)); outFile.delete();`
get("/post/:id", (req, res ) -> { //what's the difference between these 2? res.raw().setContentType("video/mp4"); res.type("video/mp4"); String id = req.params(":id"); ObjectId objectId = new ObjectId(id); BasicDBObject query = new BasicDBObject(); query.put("_id", objectId); GridFS gridfile = new GridFS(db, "videos"); GridFSDBFile gridFSDBFile = gridfile.findOne(query); res.raw().setContentLengthLong(gridFSDBFile.getLength()); InputStream inputStream = gridFSDBFile.getInputStream(); ServletOutputStream out = res.raw().getOutputStream(); int data = inputStream.read(); while (data >= 0) { gridFSDBFile.writeTo(out); data = inputStream.read(); } // out.flush(); out.close(); return 200; });
response.setDateHeader("Expires", 0); f.writeTo(os);
f.writeTo(f.getFilename()); return;
@Override public void readContent(RequestContext ctx, BinaryContentSink sink) throws Exception { GridFS gridfs = getUserspace().getGridFS(); // TODO - maybe use netty directly here? // TODO - BinaryContentSink should flush to socket (pipeline channel), not queue to memory // get tmp file File tmpFile = getTempFile(); // sync copy from db to tmp file first - no async API GridFSDBFile file = gridfs.findOne(new BasicDBObject("_id", fileInfo().getId())); file.writeTo(tmpFile); // async copy from local tmp file to sink getRoot().vertx().fileSystem().open(tmpFile.getPath(), (result) -> { if (result.succeeded()) { AsyncFile asyncFile = result.result(); asyncFile.dataHandler((buffer) -> { sink.accept(buffer.getByteBuf()); }); asyncFile.endHandler((end) -> { sink.close(); asyncFile.close(); tmpFile.delete(); }); } else { sink.close(); tmpFile.delete(); } }); }