我正在尝试写一个递归函数,它将上传一个完整的目录到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);
}
}
}发布于 2013-07-17 17:26:41
如果您想检查目录是否存在,如果不存在,则可以使用以下代码:
try {
upclient.changeDirectory(dir);
} catch(FTPException e) {
upclient.createDirectory(dir);
upclient.changeDirectory(dir);
}https://stackoverflow.com/questions/17588892
复制相似问题