遍历并导入 GridFS/MongoDB

Traverse and Import into GridFS/MongoDB

我希望整合我编写的两个程序。第一个是 I/O 流,它遍历目录并列出每个文件夹中的所有文件。第二个为 MongoDB 使用 Java 驱动程序并允许输入文件(在本例中为图像)。我想将两者结合起来,以便在遍历目录时上传文件。任何帮助表示赞赏。这是我的两个代码源:

流:

import java.io.IOException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class StreamTest {
public static void main(String[] args) {
    StreamTest streamTest = new StreamTest();
    Path readyPath = Paths.get("Ready");
    try {
        List<Path> sourceList = streamTest.listSourceFiles(readyPath);
        for (Path p : sourceList) {
            if (Files.isDirectory(p)) {
                streamTest.processFolder(p);
            }
        }
        System.out.println(sourceList);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
private void processFolder(Path p) throws IOException {
    List<Path> sourceList = listSourceFiles(p);
    Collections.sort(sourceList, new Comparator<Path>() {
        @Override
        public int compare(Path path1, Path path2) {
            if (path1.endsWith(".csv")) {
                return 1;
            } else {
                return -1;
            }
        }
    });
    System.out.println("ID = " + Math.random());
    System.out.println(sourceList);
}
List<Path> listSourceFiles(Path dir) throws IOException {
    List<Path> result = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*")) {
        for (Path entry : stream) {
            result.add(entry);
        }
    } catch (DirectoryIteratorException ex) {
        throw ex.getCause();
    }
    return result;
  }
 }

输入:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.gridfs.GridFS;
import com.mongodb.gridfs.GridFSDBFile;
import com.mongodb.gridfs.GridFSInputFile;

public class ImageDriver {

public static void main(String[] args) throws IOException {
    DB db = (new MongoClient("localhost", 27017)).getDB("JMP");
    FileInputStream inputStream = null;

    GridFS images = new GridFS(db, "images"); //Returns GidFS bucket named "images"
    try{
        inputStream = new FileInputStream("banner3.png");
    } catch (FileNotFoundException e) {
        System.out.println("Can't open file");
        System.exit(1);
    }
    GridFSInputFile image = images.createFile(inputStream, "banner3.png");

    //create some meta data for the object
    BasicDBObject meta = new BasicDBObject("description", "images and csv's");
    image.setMetaData(meta);
    image.save();

    System.out.println("Object ID in Files Collection: " + image.get("_id"));
    System.out.println("Saved the file to MongoDB");
    System.out.println("Now let's read it back out");

    GridFSDBFile gridFile = images.findOne(new BasicDBObject("filename", "banner3.png"));

    FileOutputStream outputStream = new FileOutputStream("banner3_copy.png");
    gridFile.writeTo(outputStream);

    System.out.println("write the file back out");
   }
}

将此连接到 "image driver",这是连接和插入到 Mongo 的地方...

    private void processImages(List<Path> sourceList) throws IOException { 
    //create same method but for CSV without using GridFS
    for (Path file : sourceList) {
        if (file.getFileName().toString().endsWith(".png")) {

    System.out.println(ImageDriver.uploadImage(file.toAbsolutePath()));