java getreplycode,解决:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3

[摘要:办理:org.apache.commons.net.MalformedServerReplyException: Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3   当应用org.apache.commons.net.ftp.]

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

当使用org.apache.commons.net.ftp.FTPClient通过协议SSH2进行SFTP连接时报如上错误,原因是它不支持这种方式的连接(使用FTPSClient的SSL也是不行的)。

示例代码:

package com.jerval.test;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.List;

import org.apache.commons.net.ftp.FTPClient;

import org.apache.commons.net.ftp.FTPReply;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

public class FtpFileList {

private static final Logger LOG = LoggerFactory.getLogger(FtpFileList.class);

public static void main(String[] args) {

printList();

}

private static void printList() {

List list = listFileNames("fca-vm-rds-prod1", "applog", "aaa", "/webapp/myrds1/lib");

for (String fileName:list) {

System.out.println(fileName);

}

}

private static List listFileNames(String host, String user, String password, String dir) {

List list = new ArrayList();

FTPClient ftpClient = new FTPClient();

try {

ftpClient.connect(host, 22);

int reply = ftpClient.getReplyCode();

if (FTPReply.isPositiveCompletion(reply)) {

ftpClient.login(user, password);

// ftpClient.changeWorkingDirectory(dir);

String[] names = ftpClient.listNames(dir);

list.addAll(Arrays.asList(names));

}

} catch (IOException e) {

LOG.error("ERROR!", e);

} finally {

close(ftpClient);

}

return list;

}

private static void close(FTPClient ftpClient) {

if (null != ftpClient && ftpClient.isConnected()) {

try {

ftpClient.logout();// 退出FTP服务器

} catch (IOException e) {

e.printStackTrace();

System.out.println("退出FTP服务器异常!");

System.out.println(e.getMessage());

} finally {

try {

ftpClient.disconnect();// 关闭FTP服务器的连接

System.out.println("退出并关闭FTP服务器的连接");

} catch (IOException e) {

e.printStackTrace();

System.out.println("关闭FTP服务器的连接异常!");

System.out.println(e.getMessage());

}

}

}

}

}

错误信息:

3 [main] ERROR com.jerval.test.FtpFileList - ERROR!

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.

Server Reply: SSH-2.0-OpenSSH_5.3

at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:333)

at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)

at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:396)

at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:796)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:172)

at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)

at com.jerval.test.FtpFileList.listFileNames(FtpFileList.java:32)

at com.jerval.test.FtpFileList.printList(FtpFileList.java:22)

at com.jerval.test.FtpFileList.main(FtpFileList.java:18)

org.apache.commons.net.MalformedServerReplyException: Could not parse response code.

Server Reply: Protocol mismatch.

解决方法:

使用com.jcraft.jsch.JSch提供的SSH解决方法。代码如下:

package org.jerval.test.ftp;

import java.util.ArrayList;

import java.util.List;

import java.util.Properties;

import java.util.Vector;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import com.jcraft.jsch.Channel;

import com.jcraft.jsch.ChannelSftp;

import com.jcraft.jsch.ChannelSftp.LsEntry;

import com.jcraft.jsch.JSch;

import com.jcraft.jsch.Session;

public class FtpsFileList {

private static final Logger LOG = LoggerFactory.getLogger(FtpsFileList.class);

public static void main(String[] args) {

listFileNames("fca-vm-rds-prod1.xxx.org", 22, "applog", "xxx", "/webapp/myrds1/lib");

}

private static List listFileNames(String host, int port, String username, final String password, String dir) {

List list = new ArrayList();

ChannelSftp sftp = null;

Channel channel = null;

Session sshSession = null;

try {

JSch jsch = new JSch();

jsch.getSession(username, host, port);

sshSession = jsch.getSession(username, host, port);

sshSession.setPassword(password);

Properties sshConfig = new Properties();

sshConfig.put("StrictHostKeyChecking", "no");

sshSession.setConfig(sshConfig);

sshSession.connect();

LOG.debug("Session connected!");

channel = sshSession.openChannel("sftp");

channel.connect();

LOG.debug("Channel connected!");

sftp = (ChannelSftp) channel;

Vector> vector = sftp.ls(dir);

for (Object item:vector) {

LsEntry entry = (LsEntry) item;

System.out.println(entry.getFilename());

}

} catch (Exception e) {

e.printStackTrace();

} finally {

closeChannel(sftp);

closeChannel(channel);

closeSession(sshSession);

}

return list;

}

private static void closeChannel(Channel channel) {

if (channel != null) {

if (channel.isConnected()) {

channel.disconnect();

}

}

}

private static void closeSession(Session session) {

if (session != null) {

if (session.isConnected()) {

session.disconnect();

}

}

}

}

Maven依赖:

com.jcraft

jsch

0.1.49

原文:http://www.cnblogs.com/muliu/p/6126126.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值