2013-12-19 97 views
5

我已经尝试用库commons.net创建一个项目,通过ftp发送一些文件。但我创建了与我的服务器的连接,我收到了这个错误。commons-net与ssh-2.0协议兼容

org.apache.commons.net.MalformedServerReplyException: Could not parse response code. 
Server Reply: SSH-2.0-OpenSSH_5.3 

我跟了这article为创建我的连接,并与official examples我已经控制的文章。

我的Java代码在这里:

private void connect(String host, String user, String pwd) { 
     try{ 
     ftp = new FTPSClient(false); 
     //ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out))); 
     int reply; 

     ftp.connect(host,22);//error is here 
     reply = ftp.getReplyCode(); 
     if (!FTPReply.isPositiveCompletion(reply)) { 
      ftp.disconnect(); 
      throw new Exception("Exception in connecting to FTP Server"); 
     } 
     ftp.login(user, pwd); 
     ftp.setFileType(FTP.BINARY_FILE_TYPE); 
     ftp.enterLocalPassiveMode(); 
     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
    } 

我不明白,我哪里错了。

回答

13

FTPS协议不通过SSH运行。你需要的是SFTP。对于这一点,你可以看看Jsch

JSch jsch = new JSch(); 
    Session session = jsch.getSession(user, host, port); 
    session.setConfig("PreferredAuthentications", "password"); 
    session.setPassword(pass); 
    session.connect(FTP_TIMEOUT); 
    Channel channel = session.openChannel("sftp"); 
    ChannelSftp sftp = (ChannelSftp) channel; 
    sftp.connect(FTP_TIMEOUT); 
+0

感谢尼克,正常工作与JSch库 –

+0

感谢尼克。这是非常有用的 – Motilal

3

SFTP(通过SSH连接作为SSH流运行的文件传输)与FTPS(使用SSL/TLS的FTP)不同。