【问题标题】:Traverse and Import into GridFS/MongoDB遍历并导入到 GridFS/MongoDB
【发布时间】:2015-01-13 20:53:36
【问题描述】:

我希望整合我编写的两个程序。第一个是遍历目录并列出每个文件夹中的所有文件的 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");
   }
}

【问题讨论】:

    标签: java mongodb input gridfs


    【解决方案1】:

    将此连接到“图像驱动程序”,这是连接和插入到 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()));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-04
      • 2015-08-22
      • 1970-01-01
      • 2020-01-27
      • 2016-08-12
      • 1970-01-01
      • 2019-02-03
      • 2014-04-28
      相关资源
      最近更新 更多