我创建了一个小测试程序供以后使用,它只是连接到服务器并检索文件,如果看起来像这样:
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();
}
}
}
程序打印连接成功并且文件已经存在所以它必须是检索文件。程序不会崩溃,也不会抛出任何异常。
最佳答案
我会先尝试这些:
1) 被动模式(这指示客户端建立所有连接,而不是服务器) ftp.enterLocalPassiveMode();
2) 数据超时(这应该会阻止无限期挂起,但如果您将值设置得太短,那么您可以摆脱早期缓慢的连接)它很难模拟和测试 - 只需尝试一下。 ftp.setDataTimeout(10000);//十秒
最终,有一种方法可以将 protocolMessageHandler 传递给底层套接字,因此您应该能够记录客户端和服务器之间的聊天记录。虽然我从来没有这样做过,但我想它很有可能揭示真正的问题。
关于java - FTP Apache Commons 停在 retrieveFile,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16451652/