代码:
import java.io.FileOutputStream; import org.apache.commons.net.ftp.FTPClient;
public class FtpDownloader {
// Server Credentials
String host = "ip";
String username = "user";
String password = "pass";
public static void main(String args[]) {
new FtpDownloader().downloadFile();
}
public void downloadFile() {
try {
FTPClient client_ftp = new FTPClient();
FileOutputStream fos = null;
client_ftp.connect(host);
client_ftp.login(username, password);
System.out.println("Connected : " + client_ftp.isConnected());
fos = new FileOutputStream("d://update_mac.txt");
Boolean file_got = client_ftp.retrieveFile("/update/update_mac.txt", fos);
System.out.println("Downloaded : " + file_got);
fos.close();
client_ftp.disconnect();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
正如您所看到的那样,我试图从ftp服务器下载文件,但是当我运行代码时,连接已建立但文件未下载,它在我的系统上显示0kb。您认为原因可能是什么?
提前致谢
答案 0 :(得分:0)
请务必使用其中一个参数组合进行connect()方法调用:
void connect(InetAddress host)
void connect(InetAddress host, int port)
void connect(InetAddress host, int port, InetAddress localAddr, int localPort)
void connect(String hostname)
void connect(String hostname, int port)
void connect(String hostname, int port, InetAddress localAddr, int localPort)
您可能已尝试connect("127.0.0.1")
,但这不是其中一种组合。