您可以尝试以下代码,我希望它也适用于您的情况.
该代码使用Apache Commons vsf2通过安全ftp连接(SFTP)上传文件
try {
String filepath = "";
String serverAddress = "";
String userId = "";
String password = "";
String remoteDirectory = "";
String keyPath = "";
String passPhrase = "";
File file = new File(filepath);
if (!file.exists())
throw new RuntimeException("Error. File not found");
//Initializes the file manager
StandardFileSystemManager manager = new StandardFileSystemManager();
manager.init();
//Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirI