Example usage for com.mongodb.gridfs GridFS createFile

List of usage examples for com.mongodb.gridfs GridFS createFile

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFS createFile.

Prototype

public GridFSInputFile createFile(final String filename) 

Source Link

Document

Creates a file entry.

Usage

From source file:it.marcoberri.mbfasturl.action.Commons.java

License:Apache License

/**
 * /*w  w  w  . j a  va2 s  .  co  m*/
 * @param u
 * @param dimx
 * @param dimy
 * @return
 * @throws WriterException
 * @throws IOException
 */
public static String generateQrcode(Url u, int dimx, int dimy) throws WriterException, IOException {

    final String proxy = ConfigurationHelper.getProp().getProperty("url.proxy.domain", "http://mbfu.it/");
    final GridFS fs = MongoConnectionHelper.getGridFS();

    final QRCodeWriter writer = new QRCodeWriter();
    final BitMatrix bitMatrix = writer.encode(proxy + "/" + u.getFast(), BarcodeFormat.QR_CODE, dimx, dimy);
    final File temp = File.createTempFile("tempfile" + System.currentTimeMillis(), ".tmp");
    MatrixToImageWriter.writeToFile(bitMatrix, "gif", temp);

    final GridFSInputFile gfi = fs.createFile(temp);
    gfi.setFilename(u.getFast() + ".gif");

    final BasicDBObject meta = new BasicDBObject();
    meta.put("ref_url", u.getId());
    meta.put("created", new Date());
    gfi.setMetaData(meta);

    gfi.setContentType("image/gif");
    gfi.save();

    temp.deleteOnExit();

    return gfi.getId().toString();

}

From source file:me.yyam.mongodbutils.MongoDbOperater.java

/**
 * mongodb/*from  w  w  w .j  a v  a 2s. co m*/
 * @param dbName ???
 * @param fsName  GridFS???fsnull?
 * @param input ??
 * @param fsFileName GridFS??
 * @throws FileNotFoundException
 * @throws IOException 
 */
public void upload2GridFS(String dbName, String fsName, InputStream input, String fsFileName)
        throws FileNotFoundException, IOException {
    DB db = mongoClient.getDB(dbName);
    if (fsName == null) {
        fsName = "fs";
    }
    GridFS fs = new GridFS(db, fsName);
    GridFSInputFile fsFile = fs.createFile(input);
    if (StringUtils.isBlank(fsFileName)) {
        throw new FileNotFoundException("gridfs????fsFileName");
    }
    fsFile.setFilename(fsFileName);
    fsFile.save();
}

From source file:me.yyam.mongodbutils.MongoDbOperater.java

public void upload2GridFS(String dbName, String fsName, byte[] bytes, String fsFileName)
        throws FileNotFoundException, IOException {
    DB db = mongoClient.getDB(dbName);/*ww  w  . j av  a 2  s  . c  o  m*/
    if (fsName == null) {
        fsName = "fs";
    }
    GridFS fs = new GridFS(db, fsName);
    GridFSInputFile fsFile = fs.createFile(bytes);
    if (StringUtils.isBlank(fsFileName)) {
        throw new FileNotFoundException("gridfs????fsFileName");
    }
    fsFile.setFilename(fsFileName);
    fsFile.save();
}

From source file:mx.org.cedn.avisosconagua.engine.processors.Init.java

License:Open Source License

/**
 * Processes an uploaded file and stores it in MongoDB.
 * @param item file item from the parsed servlet request
 * @param currentId ID for the current MongoDB object for the advice
 * @return file name// w  w  w .  j a v  a2s .c  o  m
 * @throws IOException 
 */
private String processUploadedFile(FileItem item, String currentId) throws IOException {
    System.out.println("file: size=" + item.getSize() + " name:" + item.getName());
    GridFS gridfs = mi.getImagesFS();
    String filename = currentId + ":" + item.getFieldName() + "_" + item.getName();
    gridfs.remove(filename);
    GridFSInputFile gfsFile = gridfs.createFile(item.getInputStream());
    gfsFile.setFilename(filename);
    gfsFile.setContentType(item.getContentType());
    gfsFile.save();
    return filename;
}

From source file:mx.org.cedn.avisosconagua.engine.processors.Pronostico.java

License:Open Source License

/**
 * Processes an uploaded file and stores it in MongoDB.
 * @param item file item from the parsed servlet request
 * @param currentId ID for the current MongoDB object for the advice
 * @return file name/*from  www .  j  ava2 s.c o m*/
 * @throws IOException 
 */
private String processUploadedFile(FileItem item, String currentId) throws IOException {
    GridFS gridfs = MongoInterface.getInstance().getImagesFS();
    GridFSInputFile gfsFile = gridfs.createFile(item.getInputStream());
    String filename = currentId + ":" + item.getFieldName() + "_" + item.getName();
    gfsFile.setFilename(filename);
    gfsFile.setContentType(item.getContentType());
    gfsFile.save();
    return filename;
}

From source file:mx.org.cedn.avisosconagua.mongo.CAPFileGenerator.java

License:Open Source License

/**
 * Generates and stores the CAP file.//  w  ww  .  j a  v a 2 s .c  om
 */
public void generate() {
    try {
        GridFS fs = MongoInterface.getInstance().getGeneratedFS();
        fs.remove(name);
        GridFSInputFile infile = fs.createFile(generator.generate().getBytes("UTF-8"));
        infile.setContentType("text/xml");
        infile.setFilename(name);
        infile.save();
        isOK = true;
    } catch (UnsupportedEncodingException uex) {
        uex.printStackTrace();
    }
}

From source file:mx.org.cedn.avisosconagua.mongo.HtmlZipGenerator.java

License:Open Source License

/**
 * Generates the ZIP file of the HTMl advice.
 *//*from  ww w . ja va 2 s  .  com*/
public void generate() {
    try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
        String localFolder = "./" + adviceID + "/";
        ZipOutputStream zout = new ZipOutputStream(baos);
        zout.setLevel(9);
        zout.putNextEntry(new ZipEntry(name));
        zout.write(html.generate(true).getBytes("ISO8859-1"));
        //zout.putNextEntry(new ZipEntry(localFolder));
        if (html.getPrincipalFile() != null) {
            GridFS gridfs = MongoInterface.getInstance().getImagesFS();
            GridFSDBFile imageForOutput = gridfs.findOne(html.getPrincipalFile());
            zout.putNextEntry(new ZipEntry(prefix + "_1"
                    + html.getPrincipalFile().substring(html.getPrincipalFile().lastIndexOf(".")))); //localFolder + 
            imageForOutput.writeTo(zout);
        }
        if (html.getPronosticoFile() != null) {
            GridFS gridfs = MongoInterface.getInstance().getImagesFS();
            GridFSDBFile imageForOutput = gridfs.findOne(html.getPronosticoFile());
            zout.putNextEntry(new ZipEntry(prefix + "_2"
                    + html.getPrincipalFile().substring(html.getPrincipalFile().lastIndexOf(".")))); //localFolder +
            imageForOutput.writeTo(zout);
        }
        zout.putNextEntry(new ZipEntry(prefix + "_f.gif"));
        InputStream fin = HtmlZipGenerator.class.getResourceAsStream("/fondo.gif");
        byte[] buff = new byte[8192];
        int lenght;
        while ((lenght = fin.read(buff)) > -1) {
            zout.write(buff, 0, lenght);
        }
        //            ArrayList<String> lista = MongoInterface.getInstance().listFilesFromAdvice(adviceID);
        //            for (String filename : lista) {
        //                GridFS gridfs = MongoInterface.getInstance().getImagesFS();
        //                GridFSDBFile imageForOutput = gridfs.findOne(filename);
        //                String fnpart[] = filename.split(":");
        //                zout.putNextEntry(new ZipEntry(localFolder + fnpart[1]));
        //                imageForOutput.writeTo(zout);
        //            }
        zout.close();
        GridFS fs = MongoInterface.getInstance().getGeneratedFS();
        fs.remove(nameZip);
        GridFSInputFile infile = fs.createFile(baos.toByteArray());
        infile.setContentType("application/zip");
        infile.setFilename(nameZip);
        infile.save();
        isOK = true;
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
}

From source file:mytubermiserver.mongo.GridFileSystem.java

public void sendVideo(String fileName, InputStream videoStream)
        throws UnknownHostException, MongoException, IOException {
    Mongo mongo = new Mongo(hostAddress, portAddress);

    DB db = mongo.getDB("MyTube");

    // create a "video" namespace
    GridFS gfsPhoto = new GridFS(db, "video");

    //get the file from stream
    GridFSInputFile gfsFile = gfsPhoto.createFile(videoStream);

    // set a new filename for identify purpose
    gfsFile.setFilename(fileName);//from  w w  w .  j  a v a  2s.co m

    // save the image file into mongoDB
    gfsFile.save();
    videoStream.close();
}

From source file:net.kamradtfamily.mongorest.GridfsServlet.java

License:GNU General Public License

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doPost()");

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];/*w  w  w  .  j  a v a  2s  . c om*/
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    String file_name = req.getParameter("filename");

    if (file_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    GridFSDBFile db_file_old = fs.findOne(file_name);
    if (db_file_old == null) {
        error(res, SC_NOT_FOUND, Status.get("file doe not exists, use PUT"));
        return;
    }

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    int n;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
    }
    os.close();

    is.close();

    out_json(req, Status.OK);

}

From source file:net.kamradtfamily.mongorest.GridfsServlet.java

License:GNU General Public License

@Override
protected void doPut(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    log.fine("doPut()");

    InputStream tmp = req.getInputStream();
    InputStream is = new BufferedInputStream(tmp);
    String db_name = req.getParameter("dbname");
    String bucket_name = req.getParameter("bucketname");
    if (db_name == null || bucket_name == null) {
        String names[] = req2mongonames(req);
        if (names != null) {
            db_name = names[0];/*from w ww  . j a v a2  s  .c om*/
            bucket_name = names[1];
        }
        if (db_name == null) {
            error(res, SC_BAD_REQUEST, Status.get("param name missing"));
            return;
        }
    }

    if (bucket_name == null)
        bucket_name = "fs";

    String file_name = req.getParameter("filename");

    if (file_name == null) {
        error(res, SC_BAD_REQUEST, Status.get("param name missing"));
        return;
    }

    DB db = mongo.getDB(db_name);

    String fs_cache_key = db_name + bucket_name;
    GridFS fs = fs_cache.get(fs_cache_key);
    if (fs == null) {
        fs = new GridFS(db, bucket_name);
        fs_cache.put(fs_cache_key, fs);
    }

    GridFSDBFile db_file_old = fs.findOne(file_name);
    if (db_file_old != null) {
        error(res, SC_BAD_REQUEST, Status.get("file already exists, use POST"));
        return;
    }

    String ct = req.getContentType();
    GridFSInputFile db_file = fs.createFile(file_name);
    if (ct != null)
        db_file.setContentType(ct);
    OutputStream os = db_file.getOutputStream();

    final int len = 4096;
    byte data[] = new byte[len];
    int n;
    while ((n = is.read(data, 0, len)) > 0) {
        os.write(data, 0, n);
    }
    os.flush();
    os.close();

    is.close();

    out_json(req, Status.OK);

}