Search code examples
javasftpjschapache-commons-vfs

org.apache.commons.vfs.FileSystemException: Could not connect to SFTP server at "sftp://username:***@114.XX.XX.XX/" on port 21


I could access SFTP from WinSCP using the below credentials

  • serverAddress=114.XX.XX.XX
  • userId=orafusion
  • password=OraP!ss!123
  • remoteDirectory=TestData/
  • Port=21

Lib used:

  • commons-vfs2-2.0.jar
  • jsch-0.1.50.jar

I get the below error when try to connect programmatically using Java. When I browsed google for the error, all I could get was special character ! is used in password. Therefore use UriParser.encode(sftpuri) that will resolve the problem but unfortunately it didn't help.

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

Below is the code I'm using for downloading file from SFTP.

Blog reference: https://www.mysamplecode.com/2013/06/sftp-apache-commons-file-download.html

Output for UriParser.encode(sftpUri). I could infer that the PORT is not appended here

sftp://orafusion:[email protected]/TestData/TestFile.txt

I get error at the below line of code

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");

Solution

  • You are using FTP in WinSCP, not SFTP. These are two completely incompatible protocols.

    Or possibly the encrypted variant of FTP, the FTPS (FTP over TLS/SSL) – what might lead you to the confusion with SFTP.

    Replace the sftp:// in your URL with ftp:// or ftps://.
    See https://commons.apache.org/proper/commons-vfs/filesystems.html#FTP