MongoDb文件上传下载

1、maven依赖


            org.mongodb
            mongo-java-driver
            3.10.2
        

2、代码

package com.busy.test;

import com.mongodb.*;
import com.mongodb.client.gridfs.GridFSBucket;
import com.mongodb.client.gridfs.GridFSBuckets;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;
import org.bson.types.ObjectId;

import java.io.*;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;

public class MongoGridFSTest {


    public static void main(String[] args) throws UnknownHostException, Exception {
        MongoClient mongoClient;
        MongoClientOptions options;
        List addresses = new ArrayList();
        ServerAddress addrTemp = new ServerAddress("192.168.1.123", 4444);
        addresses.add(addrTemp);
        MongoCredential allow = MongoCredential.createCredential("admin", "admin", "123456".toCharArray());
        MongoClientOptions.Builder build = new MongoClientOptions.Builder();
        build.connectionsPerHost(50);
        build.threadsAllowedToBlockForConnectionMultiplier(50);
        build.maxWaitTime(120000);
        build.connectTimeout(60000);
        build.socketTimeout(60000);
        options = build.build();
        mongoClient = new MongoClient(addresses, allow, options);


        FileInputStream fis = new FileInputStream("F:\\Download\\ideaIU-2019.1-jbr11.win.zip");
//        FileOutputStream fos = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\ideaIU-2019.1-jbr11.win.zip");
//        upload(mongoClient,"/test001/fileName", fis);
//        downLoad(mongoClient, "/test001/fileName", fos);
//        fis.close();
//        fos.close();
        List segments = readSegment();
        for (int i = 0; i < segments.size(); i++) {
            InputStream is = new ByteArrayInputStream(segments.get(i));
            new Thread(new UploadThread(mongoClient, "/test001/fileName[" + i + "]", is)).start();
            try {
                is.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        fis.close();

        Thread.sleep(30000);

        RandomAccessFile wFile = new RandomAccessFile("C:\\Users\\Administrator\\Desktop\\ideaIU-2019.1-jbr11.win.zip", "rw");
        System.out.println("下载开始时间:" + new Date());
        ConcurrentHashMap map = new ConcurrentHashMap();
        final CountDownLatch latch = new CountDownLatch(9);
        for (int i = 0; i < 9; i++) {
            new Thread(new DownLoadThread(map, mongoClient, "/test001/fileName[" + i + "]", latch)).start();
        }
        ByteArrayOutputStream outputStreamTemp = new ByteArrayOutputStream();
        GridFSBucket gridFSBucket = GridFSBuckets.create(mongoClient.getDatabase("yss"), "fs");
        gridFSBucket.downloadToStream("/test001/fileName[9]", outputStreamTemp);
        map.put("/test001/fileName[9]", outputStreamTemp.toByteArray());
        latch.await();
        for (int i = 0; i < 10; i++) {
            wFile.seek(wFile.length());
            wFile.write(map.get("/test001/fileName[" + i + "]"));
        }
        System.out.println("下载结束时间:" + new Date());
        wFile.close();
    }

    public static class DownLoadThread implements Runnable {
        private ConcurrentHashMap map;
        private MongoClient mongoClient;
        private String fileName;
        CountDownLatch latch;

        public DownLoadThread(ConcurrentHashMap map, MongoClient mongoClient, String fileName, CountDownLatch latch) {
            this.map = map;
            this.mongoClient = mongoClient;
            this.fileName = fileName;
            this.latch = latch;
        }

        public void run() {
            ByteArrayOutputStream outputStream = null;
            try {
                outputStream = new ByteArrayOutputStream();
                GridFSBucket gridFSBucket = GridFSBuckets.create(mongoClient.getDatabase("yss"), "fs");
                gridFSBucket.downloadToStream(fileName, outputStream);
                map.put(fileName, outputStream.toByteArray());
                latch.countDown();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static class UploadThread implements Runnable {
        private MongoClient mongoClient;
        private String fileName;
        private InputStream inputStream;

        public UploadThread(MongoClient mongoClient, String fileName, InputStream inputStream) {
            this.mongoClient = mongoClient;
            this.fileName = fileName;
            this.inputStream = inputStream;
        }

        public void run() {
            try {
                GridFSBucket gridFSBucket = GridFSBuckets.create(mongoClient.getDatabase("yss"), "fs");
                System.out.println("上传开始时间:" + new Date());
                gridFSBucket.uploadFromStream(fileName, inputStream);
                System.out.println("上传完成时间:" + new Date());
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                try {
                    this.inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void upload(MongoClient mongoClient, String fileName, InputStream inputStream) {
        GridFSBucket gridFSBucket = GridFSBuckets.create(mongoClient.getDatabase("yss"), "fs");
        System.out.println("上传开始时间:" + new Date());
        gridFSBucket.uploadFromStream(fileName, inputStream);
        System.out.println("上传完成时间:" + new Date());
    }

    public static void downLoad(MongoClient mongoClient, String fileName, OutputStream outputStream) {
        GridFSBucket gridFSBucket = GridFSBuckets.create(mongoClient.getDatabase("yss"), "fs");
        System.out.println("下载开始时间:" + new Date());
        gridFSBucket.downloadToStream(fileName, outputStream);
        System.out.println("下载完成时间:" + new Date());
    }

    public static List readSegment() {
        RandomAccessFile rFile = null;
        List listByte = new ArrayList();
        try {
            rFile = new RandomAccessFile("F:\\Download\\ideaIU-2019.1-jbr11.win.zip", "r");
            long length = rFile.length();
            long gap = rFile.length() / 10;
            for (int i = 0; i < 9; i++) {
                byte[] tempByte = new byte[(int) gap];
                rFile.seek(i * (int) gap);
                rFile.read(tempByte, 0, (int) gap);
                listByte.add(tempByte);
            }
            byte[] tempByte = new byte[(int) length - (int) gap * 9];
            rFile.seek(9 * (int) gap);
            rFile.read(tempByte, 0, (int) length - (int) gap * 9);
            listByte.add(tempByte);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                rFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return listByte;
    }
}

 

你可能感兴趣的:(NoSQL)