mongodb上传数据 java语言版

本文介绍了一种使用MongoDB作为文件存储系统的实现方法。通过Java代码示例展示了如何配置MongoDB客户端、上传本地文件到MongoDB的GridFS系统中。此方法适用于需要将大量文件高效存储于数据库的应用场景。
摘要由CSDN通过智能技术生成

mongodb 可以用于构建文件存储系统。

package cache.cache;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeoutException;

import com.mongodb.MongoClient;
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.GridFSUploadStream;

public class App2 {

    private static MongoClient mongoClient;

    public static void main(String[] args) throws IOException, TimeoutException {
        ServerAddress serverAddress = new ServerAddress("ip", 27017);
        List<ServerAddress> addrs = new ArrayList<ServerAddress>();
        addrs.add(serverAddress);

        // MongoCredential.createScramSha1Credential()三个参数分别为 用户名 数据库名称 密码
        MongoCredential credential = MongoCredential.createScramSha1Credential("zdc", "test", "12345678".toCharArray());
        List<MongoCredential> credentials = new ArrayList<MongoCredential>();
        credentials.add(credential);

        mongoClient = new MongoClient(addrs, credentials);

        // 连接到数据库
        MongoDatabase mongoDatabase = mongoClient.getDatabase("test");

        GridFSBucket gfsbk = GridFSBuckets.create(mongoDatabase);

        GridFSUploadStream uploadStream = gfsbk.openUploadStream("xxx");
//上传数据
        uploadStream.write(toByteArray2("C://Users//thinkpad//Downloads//PuTTY_0.67.0.0.exe"));

        uploadStream.flush();
        uploadStream.close();


    }


    public static byte[] toByteArray2(String filename) throws IOException {

        File f = new File(filename);
        if (!f.exists()) {
            throw new FileNotFoundException(filename);
        }

        FileChannel channel = null;
        FileInputStream fs = null;
        try {
            fs = new FileInputStream(f);
            channel = fs.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
            while ((channel.read(byteBuffer)) > 0) {
                // do nothing
                // System.out.println("reading");
            }
            return byteBuffer.array();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                channel.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值