Mongodb GridFS中對檔案的上傳、下載、刪除

來源:互聯網
上載者:User

  因為公司之前電商系統的圖片儲存用的是mongodb,所以昨天討論後把原定的fastfds改用為mongodb的gridfs。

 mongodb的分區機制也是高可用,高可擴充的。

 mongodb3.2 api地址

 maven地址:

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

下面是工具類

 
 
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     * 如果一次性讀取所有位元組,大於chunk size的可能會出現亂序,導致檔案損壞     * @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();
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.