[英]Uploading a Directory using ftp4j
我正在嘗試編寫一個遞歸函數,它將一個目錄完全上傳到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);
}
}
}
如果要檢查目錄是否存在,如果不存在,則可以使用以下代碼:
try {
upclient.changeDirectory(dir);
} catch(FTPException e) {
upclient.createDirectory(dir);
upclient.changeDirectory(dir);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.