java - 如何使用 commons-VFS 在 SFTP 期间跳过密码提示

标签 java sftp apache-commons-vfs

我正在尝试使用 apache-commons-vfs API 将文件从 Windows 上传到 Linux。我可以使用此实用程序上传文件,但是当程序运行时,它会要求输入凭据,即使它已经存在于代码中。如果我们在凭据中也输入空白,则允许上传。

是否可以跳过凭据提示?

如果 SSH 私有(private)/公共(public)是唯一的解决方案,那么请分享执行此操作的步骤。

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemOptions;
import org.apache.commons.vfs2.Selectors;
import org.apache.commons.vfs2.impl.StandardFileSystemManager;
import org.apache.commons.vfs2.provider.sftp.SftpFileSystemConfigBuilder;

public class SSHUtility {

    static Properties props;

    public static void main(String[] args) {

        SSHUtility SSHUtility = new SSHUtility();
        if (args.length < 1) {
            System.err.println("Usage: java " + SSHUtility.getClass().getName() + " Properties_file File_To_FTP ");
            System.exit(1);
        }

        String propertiesFile = args[0].trim();
        String fileToFTP = args[1].trim();
        SSHUtility.startFTP(propertiesFile, fileToFTP);

    }

    public boolean startFTP(String propertiesFilename, String fileToFTP) {

        props = new Properties();
        StandardFileSystemManager manager = new StandardFileSystemManager();

        try {

            // props.load(new FileInputStream("properties/" + propertiesFilename));
            props.load(new FileInputStream(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();

            // check if the file exists
            String filepath = localDirectory + fileToFTP;
            File file = new File(filepath);
            if (!file.exists())
                throw new RuntimeException("Error. Local file not found");

            // 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
                    + fileToFTP;

            // Create local file object
            FileObject localFile = manager.resolveFile(file.getAbsolutePath());

            System.out.println("localFile::::" + file.getAbsolutePath());

            // Create remote file object
            FileObject remoteFile = manager.resolveFile(sftpUri, opts);

            // Copy local file to sftp server
            remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
            System.out.println("File upload successful");

        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            manager.close();
        }

        return true;
    }

}

最佳答案

将 setPreferredAuthentications 设置为“publickey,keyboard-interactive,password”可以解决此问题。

// Setup our SFTP configuration
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
SftpFileSystemConfigBuilder.getInstance().setPreferredAuthentications(opts, "publickey,keyboard-interactive,password");

关于java - 如何使用 commons-VFS 在 SFTP 期间跳过密码提示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44763915/

相关文章:

java - CPU-Wise,如何优化UDP数据包发送?

ftp - 将 .netrc 与 sftp 一起使用

java - Apache Commons VFS 中的 Git 协议(protocol)

batch-file - PuTTY/PSFTP 文件传输自动化的批处理文件

c - FTPopen()、FTPclose() 等的 API 文档。等人。串联

java - 如何将 JCIFS 与 apache VFS 结合使用来访问 SMB URL?

与 apache vfs 的 SFTP 连接失败,但与 WinSCP 成功

java - 如何获取 HashMap 值中 String[] 数组的长度

java - 任何 Java "API"从 XSD 生成示例 XML?

java - NullPointerException:没有给出应用程序实例,getinstance() 甚至 this(!?) 似乎返回 null