需要使用FTP从服务器下载文件,而不使用现有库和第三个奇偶校验解决方案。我设法连接并登录到服务器,发布类型转移(ASCII)和passiv模式,所以我得到端口号并打开新的ServerSocket(端口)。但是当我调用RETR fileName时,我的程序阻塞在InputStream.readLine()上(在读取服务器端口时,意味着服务器没有响应)在调用RETR命令之前有什么东西我忘了吗?
//PASV
outputStream.println("pasv");
//227 Entering Passive Mode(a1,a2,a3,a4,p1,p2)
String response = inputStream.readLine();
// port = p1*256 + p2
ServerSocket serverSocket = new ServerSocket(port);
//RETR fileName
outputStream.println("retr "+ fileName);
//server no answer
String reply = inputStream.readLine()
答案 0 :(得分:0)
FTP PASV命令不会在客户端打开套接字,IP和端口从服务器返回客户端,基本上告诉客户端“Ok在这个IP和端口上连接我”。请查看RFC 959以获取实施细节。 JAVA中的FTP客户端实现并不是一个简单的过程。
答案 1 :(得分:0)
public void download(String remoteFile) {
FTPClient ftpClient = new FTPClient();
try {
ftpClient.connect(server, 22);
ftpClient.login(ftpUser, ftpPassword);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
// APPROACH #1: using retrieveFile(String, OutputStream)
File downloadFile1 = new File("D:/ftpdosyam.txt");
OutputStream outputStream = new BufferedOutputStream(
new FileOutputStream(downloadFile1));
boolean success = ftpClient.retrieveFile(remoteFile, outputStream);
outputStream.close();
if (success) {
System.out.println("File #1 has been downloaded successfully.");
}
} catch (SocketException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}