org.apache.commons.vfs.FileSystemException:无法连接到端口 21 上 "sftp://username:***@114.XX.XX.XX/" 的 SFTP 服务器
org.apache.commons.vfs.FileSystemException: Could not connect to SFTP server at "sftp://username:***@114.XX.XX.XX/" on port 21
我可以使用以下凭据从 WinSCP 访问 SFTP
- 服务器地址=114.XX.XX.XX
- userId=orafusion
- 密码=OraP!ss!123
- remoteDirectory=测试数据/
- 端口=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)
的输出。我可以推断这里没有附加 PORT
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 的加密变体,即 FTPS(FTP 在 TLS/SSL 之上)——什么可能导致您与 S[=22 混淆=].
将 URL 中的 sftp://
替换为 ftp://
或 ftps://
。
参见 https://commons.apache.org/proper/commons-vfs/filesystems.html#FTP
我仅在部署应用程序时收到此错误。在 IDE 本地,它工作得很好。有什么想法吗?
fun copyCsvToSFTP(fileToSFTP: String) {
val manager = StandardFileSystemManager()
try {
val file = File(fileToSFTP)
if (!file.exists()) throw FileNotFoundException("Error: Local file not found")
// Initializes the file manager
manager.init()
// Setup SFTP configuration
val options = FileSystemOptions()
setUpSftpConfiguration(options)
//Create the sftpURI using the host name, userid, password, remote path and file name
val sftpUri: String = formatUri(
userId,
password,
serverAddress,
sftpDirectory,
fileToSFTP
)
// Create local file object
val localFile: FileObject = manager.resolveFile(file.absolutePath, options)
// Create remote file object
val remoteFile: FileObject = manager.resolveFile(sftpUri, options)
// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF)
} catch (ex: Exception) {
ex.printStackTrace()
} finally {
manager.close()
}
}
@Throws(FileSystemException::class)
fun setUpSftpConfiguration(options: FileSystemOptions) {
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "no")
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false)
SftpFileSystemConfigBuilder.getInstance()
.setSessionTimeout(options, Duration.ofMillis(60000))
}
private fun formatUri(
userId: String?, password: String?, serverAddress: String?,
directory: String, fileName: String
): String {
return java.lang.String.format(
SFTP_URI_FORMAT,
URLEncoder.encode(userId, "UTF-8"),
URLEncoder.encode(password, "UTF-8"),
URLEncoder.encode(serverAddress, "UTF-8"),
URLEncoder.encode(directory, "UTF-8"),
URLEncoder.encode(fileName, "UTF-8")
)
}
我可以使用以下凭据从 WinSCP 访问 SFTP
- 服务器地址=114.XX.XX.XX
- userId=orafusion
- 密码=OraP!ss!123
- remoteDirectory=测试数据/
- 端口=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)
的输出。我可以推断这里没有附加 PORT
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 的加密变体,即 FTPS(FTP 在 TLS/SSL 之上)——什么可能导致您与 S[=22 混淆=].
将 URL 中的 sftp://
替换为 ftp://
或 ftps://
。
参见 https://commons.apache.org/proper/commons-vfs/filesystems.html#FTP
我仅在部署应用程序时收到此错误。在 IDE 本地,它工作得很好。有什么想法吗?
fun copyCsvToSFTP(fileToSFTP: String) {
val manager = StandardFileSystemManager()
try {
val file = File(fileToSFTP)
if (!file.exists()) throw FileNotFoundException("Error: Local file not found")
// Initializes the file manager
manager.init()
// Setup SFTP configuration
val options = FileSystemOptions()
setUpSftpConfiguration(options)
//Create the sftpURI using the host name, userid, password, remote path and file name
val sftpUri: String = formatUri(
userId,
password,
serverAddress,
sftpDirectory,
fileToSFTP
)
// Create local file object
val localFile: FileObject = manager.resolveFile(file.absolutePath, options)
// Create remote file object
val remoteFile: FileObject = manager.resolveFile(sftpUri, options)
// Copy local file to sftp server
remoteFile.copyFrom(localFile, Selectors.SELECT_SELF)
} catch (ex: Exception) {
ex.printStackTrace()
} finally {
manager.close()
}
}
@Throws(FileSystemException::class)
fun setUpSftpConfiguration(options: FileSystemOptions) {
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(options, "no")
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(options, false)
SftpFileSystemConfigBuilder.getInstance()
.setSessionTimeout(options, Duration.ofMillis(60000))
}
private fun formatUri(
userId: String?, password: String?, serverAddress: String?,
directory: String, fileName: String
): String {
return java.lang.String.format(
SFTP_URI_FORMAT,
URLEncoder.encode(userId, "UTF-8"),
URLEncoder.encode(password, "UTF-8"),
URLEncoder.encode(serverAddress, "UTF-8"),
URLEncoder.encode(directory, "UTF-8"),
URLEncoder.encode(fileName, "UTF-8")
)
}