我正在尝试编写一个递归函数,它将一个目录完全上传到ftp服务器上。我需要检查我上传的目录是否已经存在,所以我需要检查目录是否存在。但是在ftp4j中,我现在无法找到任何此类功能。
代码:
if(!upclient.isConnected()){
upclient.connect(ftp_ip);
upclient.login(ftp_login, ftp_pass);
}
//i want to check here if the dir exit..if no i will create one
upclient.changeDirectory(ftpupload_path);
if (upclient.isConnected()) {
System.out.println("Connected to " + ftp_ip);
File local_dir=new File(local_path);
File[] uploadFiles=local_dir.listFiles();
for (File uploadfile : uploadFiles) {
if(uploadfile.isFile()){
System.out.println("Uploading" + upclient.currentDirectory() + "/" + uploadfile.getName());
upclient.upload(uploadfile);
} else if(uploadfile.isDirectory()){
uploadDir(ftp_ip, ftp_login, ftp_pass,upclient.currentDirectory()+"/"+uploadfile.getName(), uploadfile.getAbsolutePath());
upclient.changeDirectory(ftpupload_path);
}
}
}
答案 0 :(得分:2)
如果要检查目录是否存在,如果不存在,则可以使用以下代码:
try {
upclient.changeDirectory(dir);
} catch(FTPException e) {
upclient.createDirectory(dir);
upclient.changeDirectory(dir);
}