org.apache.commons.vfs.FileSystemException:无法连接到端口 21 上"sftp://username:***@114.XX.XX.XX/"处的 SFTP 服务



我可以使用以下凭据

从WinSCP访问SFTP
  • serveraddress = 114.xx.xx.xx
  • userId = orafusion
  • 密码= orap!ss!123
  • remotedirectory = testdata/
  • port = 21

使用:

  • commons-vfs2-2.0.jar
  • JSCH-0.1.50.JAR

尝试使用Java进行编程连接时会遇到以下错误。当我浏览Google的错误时,我所能得到的只是特殊的角色!在密码中使用。因此,请使用可以解决问题的UriParser.encode(sftpuri),但不幸的是没有帮助。

org.apache.commons.vfs.FileSystemException: Could not connect to SFTP server at "sftp://orafusion:***@114.XX.XX.XX/".
    at org.apache.commons.vfs.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:99)
    at org.apache.commons.vfs.provider.AbstractOriginatingFileProvider.getFileSystem(AbstractOriginatingFileProvider.java:103)
    at org.apache.commons.vfs.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:82)
    at org.apache.commons.vfs.provider.AbstractOriginatingFileProvider.findFile(AbstractOriginatingFileProvider.java:66)
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:692)
    at org.apache.commons.vfs.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:620)
    at com.gfi.oracle.penalty.view.bean.GetMyFiles.startFTP(GetMyFiles.java:76)
    at com.gfi.oracle.penalty.view.bean.GetMyFiles.main(GetMyFiles.java:38)
Caused by: org.apache.commons.vfs.FileSystemException: Could not connect to SFTP server at "114.XX.XX.XX".
    at org.apache.commons.vfs.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:214)
    at org.apache.commons.vfs.provider.sftp.SftpFileProvider.doCreateFileSystem(SftpFileProvider.java:90)
    ... 7 more
Caused by: com.jcraft.jsch.JSchException: Session.connect: java.net.SocketTimeoutException: Read timed out
    at com.jcraft.jsch.Session.connect(Session.java:495)
    at com.jcraft.jsch.Session.connect(Session.java:150)
    at org.apache.commons.vfs.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:210)
    ... 8 more

以下是我正在使用SFTP下载文件的代码。

博客参考:https://www.mysamplecode.com/2013/06/sftp-apache-commons-file-download.html

UriParser.encode(sftpUri)输出。我可以推断该端口没有附加在这里

sftp://orafusion:OraP!ss!123@114.XX.XX.XX/TestData/TestFile.txt

我在以下代码

的下行遇到错误
FileObject remoteFile = manager.resolveFile(UriParser.encode(sftpUri), opts);

props = new Properties();
StandardFileSystemManager manager = new StandardFileSystemManager();
props.load(new FileInputStream("D:\common.properties")); // + propertiesFilename));
String serverAddress = props.getProperty("serverAddress").trim();
String userId = props.getProperty("userId").trim();
String password = props.getProperty("password").trim();
String remoteDirectory = props.getProperty("remoteDirectory").trim();
String localDirectory = props.getProperty("localDirectory").trim();
//Initializes the file manager
manager.init();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
//Create the SFTP URI using the host name, userid, password,  remote path and file name
String sftpUri = "sftp://" + userId + ":" + password +  "@" + serverAddress + "/" +
     remoteDirectory + fileToDownload;
// Create local file object
String filepath = localDirectory + fileToDownload;
File file = new File(filepath);
FileObject localFile = manager.resolveFile(file.getAbsolutePath());
System.out.println("sftp uri : " + UriParser.encode(sftpUri));
// Create remote file object
FileObject remoteFile = manager.resolveFile(UriParser.encode(sftpUri), opts);
// Copy local file to sftp serverF
localFile.copyFrom(remoteFile, Selectors.SELECT_SELF);
System.out.println("File download successful");

您在WinSCP中使用FTP,而不是SFTP。这是两个完全不兼容的协议。

或可能是FTP的加密变体,FTP(FTP/ssl上的FTP( - 什么可能导致您与SFTP混淆。

ftp://ftps://替换URL中的sftp://
请参阅https://commons.apache.org/proper/commons-vfs/filesystems.html#ftp

相关内容

  • 没有找到相关文章

最新更新