首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在使用commons-VFS的SFTP过程中跳过密码提示

如何在使用commons-VFS的SFTP过程中跳过密码提示
EN

Stack Overflow用户
提问于 2017-06-27 00:05:19
回答 1查看 1K关注 0票数 1

我正在尝试使用apache-commons-vfs API将文件从windows上传到Linux。我能够使用此实用程序上传文件,但当程序运行时,它要求输入凭据,即使它已经在代码中。如果我们还在凭证中输入空白,则允许上载。

是否可以跳过凭据提示?

如果SSH私有/公共是唯一的解决方案,那么请分享实现步骤。

代码语言:javascript
复制
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;
    }

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-07-22 02:12:21

将setPreferredAuthentications设置为“公钥、键盘交互、密码”可以解决此问题。

代码语言:javascript
复制
// 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");
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44763915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档