我尝试用库commons.net创建一个项目,通过ftp发送一些文件.但我创建了与我的服务器的连接我收到此错误.
org.apache.commons.net.MalformedServerReplyException: Could not parse response code. Server Reply: SSH-2.0-OpenSSH_5.3
我已经按照这篇文章创建了我的连接,并且通过官方示例我已经控制了文章.
我的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(); } }
我不明白我哪里出错了.
该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 );