我正在使用以下方法将文件从远程服务器 FTP 到本地机器。如果登录时的服务器没有输入参数,这可以正常工作。但是我有这个服务器,在我登录后我必须输入 1 并输入它才能进入主页。因此,当我在服务器上运行此代码时,默认情况下它会转到主页而不是工作目录。我怎样才能做到这一点?我的方法;:
public void FtpFiles() {
try {
//new ftp client
FTPClient ftp = new FTPClient();
//try to connect
ftp.connect("FtpServerName");
//login to server
if (!ftp.login("serverusername", "serverpassword")) {
ftp.logout();
}
int reply = ftp.getReplyCode();
//FTPReply stores a set of constants for FTP reply codes.
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
}
//enter passive mode
ftp.enterLocalPassiveMode();
//get system name
System.out.println("Remote system is " + ftp.getSystemType());
//change current directory
ftp.changeWorkingDirectory("/ftadm/home/users/Statements/testStatements");
System.out.println("Current Statement directory is " + ftp.printWorkingDirectory());
//get list of filenames
FTPFile[] ftpFiles = ftp.listFiles();
if (ftpFiles != null && ftpFiles.length > 0) {
//loop thru files
for (FTPFile file : ftpFiles) {
if (!file.isFile()) {
continue;
}
System.out.println("Moving File File " + file.getName());
//get output stream
OutputStream output;
output = new FileOutputStream(pr.getHomeDirName() + file.getName());
//get the file from the remote system
ftp.retrieveFile(file.getName(), output);
//close output stream
output.close();
//delete the file
if (pr.deleteRemoteFiles() == true) {
ftp.deleteFile(file.getName());
}
}
}else{
System.out.println("Remote Directory is Empty");
}
ftp.logout();
ftp.disconnect();
} catch (Exception asd) {
System.out.println(asd.getMessage());
}
}
我正在使用 Apache FTP 客户端 API