[摘要:办理: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也是不行的)。

示例代码:

  1. package com.jerval.test;
  2.  
  3. import java.io.IOException;
  4. import java.util.ArrayList;
  5. import java.util.Arrays;
  6. import java.util.List;
  7.  
  8. import org.apache.commons.net.ftp.FTPClient;
  9. import org.apache.commons.net.ftp.FTPReply;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12.  
  13. public class FtpFileList {
  14.  
  15. private static final Logger LOG = LoggerFactory.getLogger(FtpFileList.class);
  16.  
  17. public static void main(String[] args) {
  18. printList();
  19. }
  20.  
  21. private static void printList() {
  22. List<String> list = listFileNames("fca-vm-rds-prod1", "applog", "aaa", "/webapp/myrds1/lib");
  23. for (String fileName:list) {
  24. System.out.println(fileName);
  25. }
  26. }
  27.  
  28. private static List<String> listFileNames(String host, String user, String password, String dir) {
  29. List<String> list = new ArrayList<String>();
  30. FTPClient ftpClient = new FTPClient();
  31. try {
  32. ftpClient.connect(host, 22);
  33. int reply = ftpClient.getReplyCode();
  34. if (FTPReply.isPositiveCompletion(reply)) {
  35. ftpClient.login(user, password);
  36. // ftpClient.changeWorkingDirectory(dir);
  37. String[] names = ftpClient.listNames(dir);
  38. list.addAll(Arrays.asList(names));
  39. }
  40. } catch (IOException e) {
  41. LOG.error("ERROR!", e);
  42. } finally {
  43. close(ftpClient);
  44. }
  45.  
  46. return list;
  47. }
  48.  
  49. private static void close(FTPClient ftpClient) {
  50. if (null != ftpClient && ftpClient.isConnected()) {
  51. try {
  52. ftpClient.logout();// 退出FTP服务器
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. System.out.println("退出FTP服务器异常!");
  56. System.out.println(e.getMessage());
  57. } finally {
  58. try {
  59. ftpClient.disconnect();// 关闭FTP服务器的连接
  60. System.out.println("退出并关闭FTP服务器的连接");
  61. } catch (IOException e) {
  62. e.printStackTrace();
  63. System.out.println("关闭FTP服务器的连接异常!");
  64. System.out.println(e.getMessage());
  65. }
  66. }
  67. }
  68. }
  69. }

错误信息:

  1. 3 [main] ERROR com.jerval.test.FtpFileList - ERROR!
  2. org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
  3. Server Reply: SSH-2.0-OpenSSH_5.3
  4. at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:333)
  5. at org.apache.commons.net.ftp.FTP.__getReply(FTP.java:290)
  6. at org.apache.commons.net.ftp.FTP._connectAction_(FTP.java:396)
  7. at org.apache.commons.net.ftp.FTPClient._connectAction_(FTPClient.java:796)
  8. at org.apache.commons.net.SocketClient.connect(SocketClient.java:172)
  9. at org.apache.commons.net.SocketClient.connect(SocketClient.java:192)
  10. at com.jerval.test.FtpFileList.listFileNames(FtpFileList.java:32)
  11. at com.jerval.test.FtpFileList.printList(FtpFileList.java:22)
  12. at com.jerval.test.FtpFileList.main(FtpFileList.java:18)
  13. org.apache.commons.net.MalformedServerReplyException: Could not parse response code.
  14. Server Reply: Protocol mismatch.

解决方法:

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

  1. package org.jerval.test.ftp;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import java.util.Properties;
  6. import java.util.Vector;
  7.  
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10.  
  11. import com.jcraft.jsch.Channel;
  12. import com.jcraft.jsch.ChannelSftp;
  13. import com.jcraft.jsch.ChannelSftp.LsEntry;
  14. import com.jcraft.jsch.JSch;
  15. import com.jcraft.jsch.Session;
  16.  
  17. public class FtpsFileList {
  18. private static final Logger LOG = LoggerFactory.getLogger(FtpsFileList.class);
  19.  
  20. public static void main(String[] args) {
  21. listFileNames("fca-vm-rds-prod1.xxx.org", 22, "applog", "xxx", "/webapp/myrds1/lib");
  22. }
  23.  
  24. private static List<String> listFileNames(String host, int port, String username, final String password, String dir) {
  25. List<String> list = new ArrayList<String>();
  26. ChannelSftp sftp = null;
  27. Channel channel = null;
  28. Session sshSession = null;
  29. try {
  30. JSch jsch = new JSch();
  31. jsch.getSession(username, host, port);
  32. sshSession = jsch.getSession(username, host, port);
  33. sshSession.setPassword(password);
  34. Properties sshConfig = new Properties();
  35. sshConfig.put("StrictHostKeyChecking", "no");
  36. sshSession.setConfig(sshConfig);
  37. sshSession.connect();
  38. LOG.debug("Session connected!");
  39. channel = sshSession.openChannel("sftp");
  40. channel.connect();
  41. LOG.debug("Channel connected!");
  42. sftp = (ChannelSftp) channel;
  43. Vector<?> vector = sftp.ls(dir);
  44. for (Object item:vector) {
  45. LsEntry entry = (LsEntry) item;
  46. System.out.println(entry.getFilename());
  47. }
  48. } catch (Exception e) {
  49. e.printStackTrace();
  50. } finally {
  51. closeChannel(sftp);
  52. closeChannel(channel);
  53. closeSession(sshSession);
  54. }
  55. return list;
  56. }
  57.  
  58. private static void closeChannel(Channel channel) {
  59. if (channel != null) {
  60. if (channel.isConnected()) {
  61. channel.disconnect();
  62. }
  63. }
  64. }
  65.  
  66. private static void closeSession(Session session) {
  67. if (session != null) {
  68. if (session.isConnected()) {
  69. session.disconnect();
  70. }
  71. }
  72. }
  73. }

Maven依赖:

  1. <dependency>
  2. <groupId>com.jcraft</groupId>
  3. <artifactId>jsch</artifactId>
  4. <version>0.1.49</version>
  5. </dependency>

解决:Could not parse response code.Server Reply: SSH-2.0-OpenSSH_5.3的更多相关文章

  1. 解决报错Error response from daemon: Get https://10.0.0.110/v2/: dial tcp 10.0.0.110:443: connect: connection refused

    修改 #https不需要验证,否则要加上以下配置# 意思就是非安全仓库,加上重启就OK了! vim /lib/systemd/system/docker.service --insecure-regi ...

  2. 通过设置代理,解决服务器禁止抓取,报“java.io.IOException: Server returned HTTP response code: 403 for URL”错误的方法

    java.io.IOException: Server returned HTTP response code: 403 for URL: http:// 这个是什么异常呢? 当你使用java程序检索 ...

  3. 记录解决java.io.IOException: Server returned HTTP response code: 500 for URL:xxxxxxxx

    踩坑经历 因为项目需要去对接别的接口,使用URLConnection POST请求https接口,发送json数组时遇到java.io.IOException: Server returned HTT ...

  4. java.io.IOException: Server returned HTTP response code: 411 for URL

    今日调用一post方式提交的http接口,此接口在测试环境ip调用时无问题,但在生产环境通过域名调用时一直报如下错误: java.io.IOException: Server returned HTT ...

  5. Java Server returned HTTP response code: 401

    今天写一个小功能需要通过http请求获取一些返回数据,但是在登陆时是需要进行用户名和密码的校验的.写好之后请求,返回异常Java Server returned HTTP response code: ...

  6. 元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”;Syntax error on token "Invalid Character";Server returned HTTP response code: 503 for URL;

    元素 'beans' 必须不含字符 [子级], 因为该类型的内容类型为“仅元素”:复制的代码有中文空格 Syntax error on token "Invalid Character&qu ...

  7. retrying failed action with response code: 403 错误解决

    [2019-06-10T06:52:51,610][INFO ][logstash.outputs.elasticsearch] retrying failed action with respons ...

  8. Jmeter压测报错:Non HTTP response code: java.net.ConnectExceptionexception的解决办法

    前一段时间进行jmeter压测时,一直报错,查看了下日志才发现报了一堆Non HTTP response code: java.net.ConnectExceptionexception,直接jmet ...

  9. Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"

    2015-11-16 10:39:17.235 PullDemo[338:60b] Application windows are expected to have a root view contr ...

随机推荐

  1. JMeter设置集合点

    集合点:简单来理解一下,虽然我们的“性能测试”理解为“多用户并发测试”,但真正的并发是不存在的,为了更真实的实现并发这感念,我们可以在需要压力的地方设置集合点, 还拿那个用户和密码的地方,每到输入用户 ...

  2. iOS10 UI教程视图调试

    iOS10 UI教程视图调试 iOS10 UI教程视图调试,当视图很复杂的时候,层次结构就不会很简单了.Xcode可以通过视图(View)调试帮助开发者解决层次结构复杂的问题.视图调试是在Xcode ...

  3. [xsd学习]复合元素

    对于xsd,复合元素的定义有两种方式: 一.在元素内部直接声明,此种方法只能此元素使用 <xs:element name="employee"> <xs:comp ...

  4. [BZOJ2599][Race][IOI2011]点分治

    这是为了真正去学一下点分治..然后看了迪克李的ppt 又是一道写(改)了很久的题..终于ac了 1354799 orzliyicheng 2599 Accepted 31936 kb 23584 ms ...

  5. Django学习笔记之一

    一.Windows下安装 Django 1.下载安装包解压后放到本地目录如C:\Django-1.7.2 官网地址:https://www.djangoproject.com/download/ 2. ...

  6. Ue4的GitHUB版本版本管理探索

    GitHUB是学生党或者业余爱好者不错的选择,如果大家都处在一个局域网一下还是推荐用SVN,毕竟GitHUB的私有仓库要钱,而且网速难以忍受. 首先说一下:Ue4 4.10 默认生成一下文件与文件夹 ...

  7. Android开发环境搭建全程演示(jdk+eclipse+android sdk)

    全程演示android开发环境的搭建过程,无需配置环境变量.所有软件都是写该文章时最新版本 一 相关下载 (1) java JDK下载: 进入该网页: http://java.sun.com/java ...

  8. 04_Swift2基础之类型安全和类型推测+字面量+类型别名

    1. 类型安全和类型推测 1> 类型安全 Swift 是一个 _类型安全(type safe)_ 的语言.类型安全的语言可以让你清楚地知道代码要处理的值的类型.如果你的代码需要一个`String ...

  9. 同引擎mysql数据库转导快

    mysql数据库从一个表导入到另外一个表,数据库表引擎类型相同速度会快很多,相反,慢得离奇,5w,相同从myisam到myisam一或两分钟,从myisam到innodb要1到2个小时. [注意:最近 ...

  10. Storm与Spark:谁才是我们的实时处理利器

    Storm与Spark:谁才是我们的实时处理利器 ——实时商务智能目前已经逐步迈入主流,而Storm与Spark开源项目的支持无疑在其中起到了显著的推动作用.那么问题来了:实时处理到底哪家强? 实时商 ...