为什么我的ftp列表返回错误回复码425

uwopmtnx  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(611)

我正在尝试连接到z/os系统并获取数据集列表。当我执行listfiles()命令时,我得到一个425错误。我在上查找了错误和ibm站点https://www.ibm.com/support/knowledgecenter/ssltbw_2.4.0/com.ibm.zos.v2r4.cs3cod0/ftp425-04.htm 说

  1. "The session is protected by a security mechanism and the protection level for the data connection is Clear. The minimum data connection protection required by the server is Safe or Private."

我提供一个密码,我用tls和证书登录。如果我添加ftp.execpbsz(0);ftp.execprot(“p”);它给我一个无法识别的ssl消息。我在登录前和登录后都试过了,没关系。
如何执行ftp.listfiles(*)并获取有效的文件列表。
来自my pom.xml的片段

  1. <dependency>
  2. <groupId>commons-net</groupId>
  3. <artifactId>commons-net</artifactId>
  4. <version>3.7</version>
  5. </dependency>

我的java代码:

  1. private int testConnectivity(Configuration configuration) {
  2. int systemExitCode = 0;
  3. FTPSClient ftp;
  4. try {
  5. ftp = new FTPSClient(false, getSelfSignedCertificate(configuration));
  6. try {
  7. ftp.connect(configuration.getzOsStagingHostName(), configuration.getzOsStagingHostFtpPort());
  8. logger.info(ftp.getReplyString());
  9. int reply = ftp.getReplyCode();
  10. logger.info("ftp.getReplyCode =" + reply);
  11. if (!FTPReply.isPositiveCompletion(reply)) {
  12. // Bad reply code.
  13. ftp.disconnect();
  14. logger.error("FTP server refused connection.");
  15. ftp.disconnect();
  16. } else {
  17. logger.info("FTP: connected to " + configuration.getzOsStagingHostName() + ":"
  18. + configuration.getzOsStagingHostFtpPort());
  19. ftp.enterLocalPassiveMode();
  20. ftp.login(configuration.getzOsStagingUserId(), configuration.getzOsStagingUserPassword());
  21. logger.info("Login completed\n" + ftp.getStatus());
  22. //(1)
  23. //(1) ftp.execPBSZ(0);ftp.execPROT("P");
  24. ftp.enterLocalPassiveMode();
  25. logger.info("PWD=" + ftp.printWorkingDirectory());
  26. ftp.enterLocalPassiveMode();
  27. FTPFile[] ftpFiles = ftp.listFiles("*");
  28. logger.info("REPLY after list: "+Arrays.stream(ftp.getReplyStrings()).collect(Collectors.joining("\n")));
  29. for (FTPFile file : ftpFiles) {
  30. logger.info("FTP File: " + ftpFiles);
  31. }
  32. ftp.disconnect();
  33. }
  34. } catch (IOException e) {
  35. logger.error("Error connecting to z/OS host" + configuration.getzOsStagingHostName() + ":"
  36. + configuration.getzOsStagingHostFtpPort(), e);
  37. systemExitCode = 1;
  38. }
  39. } catch (KeyManagementException | CertificateException | KeyStoreException | NoSuchAlgorithmException
  40. | IOException e1) {
  41. systemExitCode = 1;
  42. logger.error("An error occurred getting a certifcate for host", e1);
  43. }
  44. return systemExitCode;
  45. }
  46. private SSLContext getSelfSignedCertificate(Configuration configuration) throws CertificateException,
  47. KeyStoreException, NoSuchAlgorithmException, IOException, KeyManagementException {
  48. File crtFile = new File(configuration.getzOsStagingHostCertificateFileName());
  49. Certificate certificate = CertificateFactory.getInstance("X.509")
  50. .generateCertificate(new FileInputStream(crtFile));
  51. KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
  52. keyStore.load(null, null);
  53. keyStore.setCertificateEntry("server", certificate);
  54. TrustManagerFactory trustManagerFactory = TrustManagerFactory
  55. .getInstance(TrustManagerFactory.getDefaultAlgorithm());
  56. trustManagerFactory.init(keyStore);
  57. SSLContext sslContext = SSLContext.getInstance("TLS");
  58. sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
  59. return sslContext;
  60. }

注意。如果我取消对标有(1)的行的注解,我将得到

  1. javax.net.ssl.SSLException: Unsupported or unrecognized SSL message
  2. at sun.security.ssl.SSLSocketInputRecord.handleUnknownRecord(SSLSocketInputRecord.java:448) ~[?:1.8.0_271]
  3. at sun.security.ssl.SSLSocketInputRecord.decode(SSLSocketInputRecord.java:174) ~[?:1.8.0_271]
  4. at sun.security.ssl.SSLTransport.decode(SSLTransport.java:110) ~[?:1.8.0_271]
  5. at sun.security.ssl.SSLSocketImpl.decode(SSLSocketImpl.java:1279) ~[?:1.8.0_271]
  6. at sun.security.ssl.SSLSocketImpl.readHandshakeRecord(SSLSocketImpl.java:1188) ~[?:1.8.0_271]
  7. at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:401) ~[?:1.8.0_271]
  8. at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:373) ~[?:1.8.0_271]
  9. at org.apache.commons.net.ftp.FTPSClient._openDataConnection_(FTPSClient.java:642) ~[commons-net-3.7.jar:3.7]
  10. at org.apache.commons.net.ftp.FTPClient._openDataConnection_(FTPClient.java:790) ~[commons-net-3.7.jar:3.7]
  11. at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3456) ~[commons-net-3.7.jar:3.7]
  12. at org.apache.commons.net.ftp.FTPClient.initiateListParsing(FTPClient.java:3386) ~[commons-net-3.7.jar:3.7]
  13. at org.apache.commons.net.ftp.FTPClient.listFiles(FTPClient.java:3063) ~[commons-net-3.7.jar:3.7]
  14. at com.mycompany.myprod.sprint.App.testConnectivity(App.java:215) [classes/:?]
  15. at com.mycompany.myprod.sprint.App.<init>(App.java:90) [classes/:?]
  16. at com.mycompany.myprod.sprint.App.main(App.java:65) [classes/:?]
wribegjk

wribegjk1#

这个问题是由于使用了一个旧版本的commons-net。我从3.7调到3.7.2,它就消失了。如果有人在看,我在登录后调用ftp.execpbsz(0)和ftp.execprot(“p”)。

相关问题