Uploading, downloading and deleting files in Mongodb Gridfs

Source: Internet
Author: User
Tags file info int size mongoclient mongodb static class uuid


Because the company before the electronic business system image storage used is MongoDB, so yesterday after the discussion of the original Fastfds switch to MongoDB Gridfs.



MongoDB's fragmentation mechanism is also highly available and highly scalable.



mongodb3.2 API Address



Maven Address:





<dependency>
    <groupId>org.mongodb</groupId>
    <artifactid>mongo-java-driver</ artifactid>
    <version>3.2.2</version>
</dependency>





Here is the tool class







package com.yjy.dg.app.log.mongo;

import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import com.mongodb.client.MongoDatabase;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.client.gridfs.GridFSDownloadStream;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.yjy.dg.app.log.utils.CommonUtil;
import org.bson.types.ObjectId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io. *;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import java.util.regex.Pattern;

/ **
 * MongoDbUtil use mongo-java-driver-3.2.2 gridfs
 *
 * @author littlehow
 * @time 2016-07-19 9:18
 * /
public class MongoDbUtil {
    private MongoDbUtil () {}
    private final static Properties properties = new Properties ();
    private static Logger log = LoggerFactory.getLogger (MongoDbUtil.class);
    static {
        try {
            URL url = MongoDbUtil.class.getClassLoader (). GetResource ("mongo_db.properties");
            if (url! = null) {
                log.info ("Found 'mongo_db.properties' file in local classpath");
                InputStream in = url.openStream ();
                try {
                    properties.load (in);
                } finally {
                    in.close ();
                }
            }
        } catch (IOException e) {
            log.info ("Could not load 'mongo_db.properties' file from local classpath:" + e);
        }
    }

    / **
     * config file info
     * /
    private static class Config {
        // mongodb connection properties
        public static String ip = null;
        public static int port = 27017; // default port is 27017
        public static String database = null;
        // mongodb connection pool properties
        public static int connectionsPerHost = 10;
        public static int maxWaitTime = 120000;
        public static int connectTimeout = 0;
        public static MongoClientOptions options = null;
        // author
        public static List <MongoCredential> credential = new ArrayList <> ();
        static {
            ip = properties.getProperty ("mongo_ip");
            database = properties.getProperty ("mongo_database");
            int _port = CommonUtil.getIntValue (properties.getProperty ("mongo_port"));
            if (_port! = -1) port = _port;
            int _conn = CommonUtil.getIntValue (properties.getProperty ("connections_per_host"));
            if (_conn! = -1) connectionsPerHost = _conn;
            int _waittime = CommonUtil.getIntValue (properties.getProperty ("max_wait_time"));
            if (_waittime! = -1) maxWaitTime = _waittime;
            int _timeout = CommonUtil.getIntValue (properties.getProperty ("connect_timeout"));
            if (_timeout! = -1) connectTimeout = _timeout;
            options = MongoClientOptions.builder (). connectTimeout (connectTimeout)
                    .maxWaitTime (maxWaitTime) .connectionsPerHost (connectionsPerHost) .build ();
            MongoCredential credential1 = MongoCredential.createCredential (properties.getProperty ("mongo_user"),
                    database, properties.getProperty ("mongo_pass"). toCharArray ());
            credential.add (credential1);
        }
    }

    private static final class MongoInstance {
        public final static MongoClient client;
        static {
            client = new MongoClient (new ServerAddress (Config.ip, Config.port), Config.credential, Config.options);
        }
    }

    / **
     * destroy pool
     * /
    public static final void destroy () {
        MongoInstance.client.close ();
    }

    / **
     * get a MongoDatabase
     * @return
     * /
    public static MongoDatabase getDatabase () {
        return MongoInstance.client.getDatabase (Config.database);
    }

    / **
     * get a MongoDatabase by Name
     * @param databaseName
     * @return
     * /
    public static MongoDatabase getDatabase (String databaseName) {
        return MongoInstance.client.getDatabase (databaseName);
    }

    / **
     * upload file to mongo
     * @param filename
     * @param in
     * @return
     * /
    public static String uploadFileToGridFS (String filename, InputStream in) {
        // default bucket name is fs
        GridFSBucket bucket = GridFSBuckets.create (getDatabase ());
        ObjectId fileId = bucket.uploadFromStream (filename, in);
        return fileId.toHexString ();
    }

    / **
     * upload file to mongo, if close is true then close the inputstream
     * @param filename
     * @param in
     * @param close
     * @return
     * /
    public static String uploadFileToGridFS (String filename, InputStream in, boolean close) {
        String returnId = null;
        try {
            returnId = uploadFileToGridFS (filename, in);
        } finally {
            if (close) {
                try {
                    in.close ();
} catch (IOException e) {
                    log.info ("close inputstream fail:" + e);
                }
            }
        }
        return returnId;
    }

    / **
     * upload file to mongo
     * @param fileName
     * @param file
     * @return
     * /
    public static String uploadFileToGridFs (String fileName, File file) {
        InputStream in = null;
        try {
            in = new FileInputStream (file);
            String returnId = uploadFileToGridFS (fileName, in, true);
            return returnId;
        } catch (IOException e) {
            log.info ("upload fail:" + e);
        }
        return null;
    }

    / **
     * set filename = file name
     * @param file
     * @return
     * /
    public static String uploadFileToGridFs (File file) {
        return uploadFileToGridFs (file.getName (), file);
    }

    / **
     * set filename = uuid
     * @param file
     * @return
     * /
    public static String uploadFileToGridFSByUUID (File file) {
        return uploadFileToGridFs (UUID.randomUUID (). toString (), file);
    }

    / **
     * download file for gridfs by objectid
     * @param objectId
     * @param out
     * /
    public static void downloadFile (String objectId, OutputStream out) {
        GridFSBucket bucket = GridFSBuckets.create (getDatabase ());
        bucket.downloadToStream (new ObjectId (objectId), out);
    }

    / **
     * download file for gridfs by objectid
     * @param objectId
     * @param file
     * /
    public static void downloadFile (String objectId, File file) {
        OutputStream os = null;
        try {
            os = new FileOutputStream (file);
            downloadFile (objectId, os);
        } catch (IOException e) {
            log.info ("download fail:" + e);
        } finally {
            if (os! = null) {
                try {
                    os.close ();
                } catch (IOException e) {
                    log.info ("close outputstream fail:" + e);
                }
            }
        }
    }

    / **
     * download file for gridfs by objectid
     * @param objectId
     * @param filename
     * /
    public static void downloadFile (String objectId, String filename) {
        File file = new File (filename);
        downloadFile (objectId, file);
    }

    / **
     * download file for gridfs by filename
     * @param filename
     * @param out
     * /
    public static void downloadFileByName (String filename, OutputStream out) {
        GridFSBucket bucket = GridFSBuckets.create (getDatabase ());
        bucket.downloadToStreamByName (filename, out);
    }

    / **
     * download file for gridfs use stream
     * If all bytes are read at once, the order larger than the chunk size may appear out of order, resulting in file corruption
     * @param objectId
     * @param out
     * /
    public static void downloadFileUseStream (String objectId, OutputStream out) {
        GridFSBucket bucket = GridFSBuckets.create (getDatabase ());
        GridFSDownloadStream stream = null;
        try {
            stream = bucket.openDownloadStream (new ObjectId (objectId));
            / ** gridfs file * /
            GridFSFile file = stream.getGridFSFile ();
            / ** chunk size * /
            int size = file.getChunkSize ();
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.