Example usage for com.mongodb.gridfs GridFSDBFile getInputStream

List of usage examples for com.mongodb.gridfs GridFSDBFile getInputStream

Introduction

In this page you can find the example usage for com.mongodb.gridfs GridFSDBFile getInputStream.

Prototype

public InputStream getInputStream() 

Source Link

Document

Returns an InputStream from which data can be read.

Usage

From source file:calliope.db.MongoConnection.java

License:Open Source License

/**
 * Get an image from the database// w ww  .j  av a2  s .c o m
 * @param collName the collection name
 * @param docID the docid of the corpix
 * @return the image data
 */
@Override
public byte[] getImageFromDb(String collName, String docID) {
    try {
        connect();
        GridFS gfs = new GridFS(db, collName);
        GridFSDBFile file = gfs.findOne(docID);
        if (file != null) {
            InputStream ins = file.getInputStream();
            long dataLen = file.getLength();
            // this only happens if it is > 2 GB
            if (dataLen > Integer.MAX_VALUE)
                throw new AeseException("file too big (size=" + dataLen + ")");
            byte[] data = new byte[(int) dataLen];
            int offset = 0;
            while (offset < dataLen) {
                int len = ins.available();
                offset += ins.read(data, offset, len);
            }
            return data;
        } else
            throw new FileNotFoundException(docID);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        return null;
    }
}

From source file:cfel.service.DatabaseServive.java

License:Open Source License

public boolean readFile(String id, OutputStream os) throws IOException {
    GridFSDBFile dbFile = getFile(id);
    if (dbFile == null) {
        return false;
    }/*from  w  w  w. j av a  2 s .c o  m*/
    InputStream is = null;
    try {
        is = dbFile.getInputStream();
        byte data[] = new byte[4096];
        int len = 0;
        while ((len = is.read(data, 0, data.length)) > 0) {
            os.write(data, 0, len);
        }
        return true;
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:cn.vlabs.clb.server.ui.frameservice.document.handler.GetMultiDocsContentHandler.java

License:Apache License

private void readDocuments(List<DocVersion> dvlist, String zipFileName, HttpServletRequest request,
        HttpServletResponse response) throws FileContentNotFoundException {
    DocumentFacade df = AppFacade.getDocumentFacade();
    OutputStream os = null;/*w  ww.j  a  va 2 s  .com*/
    InputStream is = null;
    ZipArchiveOutputStream zos = null;
    try {
        if (dvlist != null) {
            response.setCharacterEncoding("utf-8");
            response.setContentType("application/zip");
            String headerValue = ResponseHeaderUtils.buildResponseHeader(request, zipFileName, true);
            response.setHeader("Content-Disposition", headerValue);
            os = response.getOutputStream();
            zos = new ZipArchiveOutputStream(os);
            zos.setEncoding("utf-8");
            Map<String, Boolean> dupMap = new HashMap<String, Boolean>();
            GridFSDBFile dbfile = null;
            int i = 1;
            for (DocVersion dv : dvlist) {
                dbfile = df.readDocContent(dv);
                if (dbfile != null) {
                    String filename = dbfile.getFilename();
                    ZipArchiveEntry entry = null;
                    if (dupMap.get(filename) != null) {
                        entry = new ZipArchiveEntry((i + 1) + "-" + filename);

                    } else {
                        entry = new ZipArchiveEntry(filename);
                    }
                    entry.setSize(dbfile.getLength());
                    zos.putArchiveEntry(entry);
                    is = dbfile.getInputStream();
                    FileCopyUtils.copy(is, zos);
                    zos.closeArchiveEntry();
                    dupMap.put(filename, true);
                }
                i++;
            }
            zos.finish();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(zos);
        IOUtils.closeQuietly(os);
        IOUtils.closeQuietly(is);
    }
}

From source file:cn.vlabs.clb.server.ui.frameservice.image.ImageFacade.java

License:Apache License

public SrcPair loadOriginalImage(GridFSDBFile gfile) {
    String srcStorageKey = gfile.get("storageKey").toString();
    SrcPair sp = localCacheService.saveTempFile(srcStorageKey, gfile.getInputStream());
    return sp;//from w  w  w  .j a  v a 2s  . c om
}

From source file:cn.vlabs.clb.server.ui.frameservice.LocalFileCacheService.java

License:Apache License

public SrcPair loadOriginalImage(GridFSDBFile gfile) {
    String srcStorageKey = gfile.get("storageKey").toString();
    SrcPair sp = saveTempFile(srcStorageKey, gfile.getInputStream());
    return sp;/*from w ww  .j  av  a2s .  c  o m*/
}

From source file:cn.vlabs.clb.server.web.IndexController.java

License:Apache License

@RequestMapping("/batchResizeExistImages")
@ResponseBody/*from  www. j av a2  s. c om*/
public JSONObject batchResizeExistImages(@RequestParam("appid") int appid,
        @RequestParam("startTime") String startTimeStr, @RequestParam("endTime") String endTimeStr) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
    try {
        Date startTime = sdf.parse(startTimeStr);
        Date endTime = sdf.parse(endTimeStr);
        if (startTime != null) {
            List<ImageItem> items = imageService.readAll(appid, startTime, endTime);
            int i = 0;
            LOG.info("There are " + items.size() + " records.");
            long start1 = System.currentTimeMillis();
            for (ImageItem item : items) {
                long start = System.currentTimeMillis();
                i++;
                DPair p = new DPair(appid, item.getDocid(), item.getVersion());
                String skey = item.getStorageKey();
                imf.removeImage(skey);
                try {
                    GridFSDBFile gbfile = df.readDocContent(p);
                    if (gbfile != null) {
                        SrcPair sp = localCacheService.saveTempFile(item.getStorageKey(),
                                gbfile.getInputStream());
                        imf.addEvent(sp, item);
                        long end = System.currentTimeMillis();
                        LOG.info("Finish send a new resize event. There are (" + i + "/" + items.size()
                                + ") jobs to send. Use time " + (end - start) + "ms");
                    }
                } catch (FileContentNotFoundException e) {
                    LOG.info("The original file is not found." + item.getUniqueKey());
                }
            }
            long end1 = System.currentTimeMillis();
            LOG.info("The whole job for refresh " + items.size() + " images use time " + (end1 - start1)
                    + " ms.");
        }
    } catch (ParseException e1) {
        LOG.error("Date parse error", e1);
    }
    JSONObject json = new JSONObject();
    json.put("status", "success");
    return json;
}

From source file:com.bugull.mongo.fs.ImageUploader.java

License:Apache License

private InputStream getOriginalInputStream() {
    DBObject query = new BasicDBObject(BuguFS.FILENAME, filename);
    query.put(DIMENSION, null);//ww  w.j a v a 2 s .c o  m
    BuguFS fs = BuguFSFactory.getInstance().create(bucketName, chunkSize);
    GridFSDBFile f = fs.findOne(query);
    return f.getInputStream();
}

From source file:com.bugull.mongo.fs.UploadedFileServlet.java

License:Apache License

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (!StringUtil.isEmpty(password)) {
        String p = request.getParameter("password");
        if (StringUtil.isEmpty(p) || !p.equals(password)) {
            return;
        }//ww w  . jav  a 2  s  .  c o  m
    }
    String uri = request.getRequestURI();
    int second = uri.indexOf(SLASH, 1);
    uri = uri.substring(second);
    int last = uri.lastIndexOf(SLASH);
    String filename = uri.substring(last + 1);
    DBObject query = new BasicDBObject(BuguFS.FILENAME, filename);
    query.put(ImageUploader.DIMENSION, null); //note: this is necessary!
    String bucketName = GridFS.DEFAULT_BUCKET;
    int first = uri.indexOf(SLASH);
    if (first != last) {
        String sub = uri.substring(first + 1, last);
        String[] arr = sub.split(SLASH);
        for (int i = 0; i < arr.length; i += 2) {
            if (arr[i].equals(BuguFS.BUCKET)) {
                bucketName = arr[i + 1];
            } else {
                query.put(arr[i], arr[i + 1]);
            }
        }
    }
    //check if the bucket is allowed to access by this servlet
    if (!StringUtil.isEmpty(allowBucket) && !allowBucket.equalsIgnoreCase(bucketName)) {
        return;
    }
    if (!StringUtil.isEmpty(forbidBucket) && forbidBucket.equalsIgnoreCase(bucketName)) {
        return;
    }
    BuguFS fs = BuguFSFactory.getInstance().create(bucketName);
    GridFSDBFile f = fs.findOne(query);
    if (f == null) {
        return;
    }
    OutputStream os = response.getOutputStream();
    int fileLength = (int) f.getLength();
    String ext = StringUtil.getExtention(filename);
    response.setContentType(getContentType(ext));
    String range = request.getHeader("Range");
    //normal http request, no "range" in header.
    if (StringUtil.isEmpty(range)) {
        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentLength(fileLength);
        if (needCache(ext)) {
            String modifiedSince = request.getHeader("If-Modified-Since");
            DateFormat df = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);
            df.setTimeZone(TimeZone.getTimeZone("GMT"));
            Date uploadDate = f.getUploadDate();
            String lastModified = df.format(uploadDate);
            if (modifiedSince != null) {
                Date modifiedDate = null;
                Date sinceDate = null;
                try {
                    modifiedDate = df.parse(lastModified);
                    sinceDate = df.parse(modifiedSince);
                } catch (ParseException ex) {
                    logger.error("Can not parse the Date", ex);
                }
                if (modifiedDate.compareTo(sinceDate) <= 0) {
                    response.setStatus(304); //Not Modified
                    return;
                }
            }
            long maxAge = 365L * 24L * 60L * 60L; //one year, in seconds
            response.setHeader("Cache-Control", "max-age=" + maxAge);
            response.setHeader("Last-Modified", lastModified);
            response.setDateHeader("Expires", uploadDate.getTime() + maxAge * 1000L);
        } else {
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
        }
        f.writeTo(os);
    }
    //has "range" in header
    else {
        range = range.substring("bytes=".length());
        if (StringUtil.isEmpty(range)) {
            return;
        }
        int begin = 0;
        int end = fileLength - 1;
        boolean onlyLast = range.startsWith("-");
        String[] rangeArray = range.split("-");
        if (rangeArray.length == 1) {
            if (onlyLast) {
                begin = fileLength - Integer.parseInt(rangeArray[0]);
            } else {
                begin = Integer.parseInt(rangeArray[0]);
            }
        } else if (rangeArray.length == 2) {
            begin = Integer.parseInt(rangeArray[0]);
            end = Integer.parseInt(rangeArray[1]);
        }
        response.setStatus(HttpServletResponse.SC_PARTIAL_CONTENT);
        int contentLength = end - begin + 1;
        response.setContentLength(contentLength);
        response.setHeader("Content-Range", "bytes " + begin + "-" + end + "/" + contentLength);
        InputStream is = f.getInputStream();
        is.skip(begin);
        int read = -1;
        int bufferSize = (int) f.getChunkSize();
        byte[] buffer = new byte[bufferSize];
        int remain = contentLength;
        int readSize = Math.min(bufferSize, remain);
        while ((read = is.read(buffer, 0, readSize)) != -1) {
            os.write(buffer, 0, read);
            remain -= read;
            if (remain <= 0) {
                break;
            }
            readSize = Math.min(bufferSize, remain);
        }
        StreamUtil.safeClose(is);
    }
    StreamUtil.safeClose(os);
}

From source file:com.buzz.buzzdata.MongoBuzz.java

@Override
public InputStream GetImgByBuzz(String buzz_id, int pic_num) {
    InputStream retval = null;//from  ww w  . j  a  va2  s .co m
    GridFS gridFS = new GridFS(mongoDB);
    ObjectId _id = new ObjectId(buzz_id);
    BasicDBObject buzz_query = new BasicDBObject("BuzzID", _id);
    GridFSDBFile file = null;
    List<GridFSDBFile> files = gridFS.find(buzz_query);
    for (GridFSDBFile file_buzz : files) {
        if ((int) file_buzz.get("PicNum") == pic_num) {
            file = file_buzz;
        }
    }
    retval = file.getInputStream();
    return retval;
}

From source file:com.cloudbees.demo.beesshop.service.MongoDbFileStorageService.java

License:Apache License

public void serveFile(String photoId, String id, HttpServletResponse response) throws IOException {

    GridFSDBFile file = gridFS.findOne(new ObjectId(id));
    response.setContentType(file.getContentType());
    response.setHeader("cache-control", "public, max-age=" + TimeUnit.SECONDS.convert(365, TimeUnit.DAYS));
    try (InputStream in = file.getInputStream()) {
        ByteStreams.copy(in, response.getOutputStream());
    }//w ww .  ja v a2s.  c  o  m

}