我的问题是我正在创建一个FTP客户端,到目前为止它的工作完美无缺,除了一个小细节外,还会让我烦恼。 我需要知道FTP欢迎信息跨越多少行......而这是不能接受的!如何知道FTP的结束欢迎信息
private Socket connection;
private PrintWriter outStream;
private Scanner inStream;
public void InitiateConnection() throws IOException
{
log.Info(this, "Initiating connection to host: " + host + ":" + port);
connection = new Socket(host, port);
log.Info(this, "Connection initiated.");
outStream = new PrintWriter(connection.getOutputStream(), true);
inStream = new Scanner(connection.getInputStream());
Listen();
Listen();
Listen();
}
public String Listen() throws IOException
{
if(connection == null)
throw new IOException("Connection not initiated yet");
String response = inStream.nextLine();
log.Info(this, "Response: " + response);
return response;
}
这是简单的设置,我省略了所有其他代码,因为它与我的问题没有任何关系。
我已经尝试了多种方法来尝试实现这一点。 失败解决方法1:
String response = "";
while(response != null)
Listen();
失败解决方法2:
while(connection.getInputStream().available > 0)
Listen();
和无数其他人......但无论它不工作,或者方法阻塞并等待新的输入。我甚至尝试了超时,但这并不完美无缺,它不是一个适当的解决方案,这个问题...
我需要能够从FTP服务器获得整个欢迎消息,而不知道线的量...... 因此,我既可以得到这样的:
Response: 220-FileZilla Server version 0.9.39 beta
Response: 220-written by Tim Kosse ([email protected])
Response: 220 Please visit http://sourceforge.net/projects/filezilla/
这:
Response: 220-FileZilla Server version 0.9.40 beta
Response: 220 Welcome to Andrés FTP Server
这是*多行*回复格式。来自同一部分:“一个FTP回复包含一个三位数字(以三个字母数字字符发送),后面跟着一些文本。”从规范中不清楚,但如果启动FileZilla,您将看到连接过程中唯一的ML响应是FEAT命令中的211响应。 – linski
会相应更新:) – linski