Java 遍历并导入GridFS/MongoDB
我希望整合我写的两个程序。第一个是I/O流,它遍历一个目录并列出每个文件夹中的所有文件。第二种方法使用MongoDB的Java驱动程序,并允许输入文件(在本例中为图像)。我想将两者结合起来,以便在遍历目录时,文件在遇到它们时被上传。感谢您的帮助。以下是我的两个代码源: 流:Java 遍历并导入GridFS/MongoDB,java,mongodb,input,gridfs,Java,Mongodb,Input,Gridfs,我希望整合我写的两个程序。第一个是I/O流,它遍历一个目录并列出每个文件夹中的所有文件。第二种方法使用MongoDB的Java驱动程序,并允许输入文件(在本例中为图像)。我想将两者结合起来,以便在遍历目录时,文件在遇到它们时被上传。感谢您的帮助。以下是我的两个代码源: 流: import java.io.IOException; import java.nio.file.DirectoryIteratorException; import java.nio.file.DirectoryStrea
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;
}
}
将此连接到“映像驱动程序”,这是连接和插入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()));
private void processImages(List sourceList)抛出IOException{
//创建相同的方法,但不使用GridFS为CSV创建
用于(路径文件:sourceList){
if(file.getFileName().toString().endsWith(“.png”)){
System.out.println(ImageDriver.uploadImage(file.toabsolutionpath());
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()));