我创建了一个小测试程序供以后使用,它只是连接到服务器并检索文件,如果看起来像这样:
import org.apache.commons.net.ftp.*;
import java.io.*;
import java.net.SocketException;
public class Test {
private final String pass = [I removed this password];
FTPClient ftp;
File file = new File("download.txt");
private int reply;
FileOutputStream dfile;
public void ftp() {
try {
ftp = new FTPClient();
ftp.connect("ftp.bevilacqua.me");
ftp.login([i removed this username] ,[I removed this password]);
reply = ftp.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)) {
System.out.println("Connected Success");
} else {
System.out.println("Connection unsuccessful");
ftp.disconnect();
}
if(file.exists()) {
System.out.println("File already exists");
}
dfile = new FileOutputStream(file);
ftp.retrieveFile("untitled.txt", dfile); //Untitled does exist in directory '/'
System.out.println("Success... maybe");
} catch(SocketException ex) {
ex.printStackTrace();
} catch(IOException ex) {
ex.printStackTrace();
}
}
public static void main(String[] args) {
Test main = new Test();
main.ftp();
try {
main.ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
程序打印 Connected Success 并且 File already exists 所以它必须是检索文件。程序不会崩溃,也不会抛出异常。