1. 程式人生 > >解決:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3

解決: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.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<String> list = listFileNames("fca-vm-rds-prod1", "applog", "aaa", "/webapp/myrds1/lib");
        for (String fileName:list) {
            System.out.println(fileName);
        }
    }

    private static List<String> listFileNames(String host, String user, String password, String dir) {
        List<String> list = new ArrayList<String>();
        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)

解決方法:

使用com.jcraft.jsch.JSch提供的SSH解決方法程式碼如下:








Maven的依賴: