简体   繁体   English

使用 Apache Commons VFS-SFTP,上传到服务器

[英]Using Apache Commons VFS- SFTP, uploading to a server

I'm attempting to use Apache Commons VFS to SFTP a file onto a server, but I keep getting the following error:我正在尝试使用 Apache Commons VFS 将文件通过 SFTP 传输到服务器上,但我不断收到以下错误:

java.lang.RuntimeException: org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at "sftp://user:***@xxx.x.xxx.xxx/".

Is it normal for it to not include the remote file path ( remoteFilePath ) here?此处不包含远程文件路径 ( remoteFilePath ) 是否正常? It's in my code to include it in the connection string (see below)在我的代码中将它包含在连接字符串中(见下文)

I have the following jars included in my pom :我的 pom 中包含以下 jars

  • commons-logging-1.1.3.jar commons-logging-1.1.3.jar

  • commons-vfs2-2.0.jar commons-vfs2-2.0.jar

  • hamcrest-core-1.3.jar hamcrest-core-1.3.jar

  • jsch-0.1.50.jar jsch-0.1.50.jar

Code :代码

public void SftpMethod(String strMsg, String tableName){

    String host = "xxx.x.xxx.xxx";
    String user = "user";
    String pass = "password!";
    String localFilePath = "C:\\Users\\exampleDir\\Desktop\\loc.dat";
    String remoteFilePath = "/dir/home/user/export/loc.dat";
    StandardFileSystemManager manager = new StandardFileSystemManager();

    File file = new File(localFilePath);

    if (!file.exists())
        throw new RuntimeException("Error. Local file not found");

    try{
        manager.init();
        // Create local file object
        FileObject localFile = manager.resolveFile(file.getAbsolutePath());
        // Create remote file object
        FileObject remoteFile = manager.resolveFile(
                createConnectionString(host, user, pass, remoteFilePath), 
                createDefaultOptions());
        // Copy local file to SFTP server
        remoteFile.copyFrom(localFile, Selectors.SELECT_SELF);
        System.out.println("File upload success");

    }catch(IOException e){
        throw new RuntimeException(e);
    }finally{
        manager.close();
    }
}

public static String createConnectionString(String hostName, String username, String password, String remoteFilePath) {
    return "sftp://" + username + ":" + password + "@" + hostName + "/" + remoteFilePath;
}

public static FileSystemOptions createDefaultOptions() throws FileSystemException {
    // Create SFTP options
    FileSystemOptions opts = new FileSystemOptions();

    // SSH Key checking
    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(opts, "no");

    /*
     * Using the following line will cause VFS to choose File System's Root
     * as VFS's root. If I wanted to use User's home as VFS's root then set
     * 2nd method parameter to "true"
     */
    // Root directory set to user home
    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);

    // Timeout is count by Milliseconds
    SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);

    return opts;
}

Without the full stack trace its hard to give a conclusive answer, but this is what I saw recently:如果没有完整的堆栈跟踪,很难给出结论性的答案,但这是我最近看到的:

Caused by: org.apache.commons.vfs2.FileSystemException: Could not load private key from "/Users/<user>/.ssh/id_rsa".
    at org.apache.commons.vfs2.provider.sftp.SftpClientFactory.createConnection(SftpClientFactory.java:131)

Unfortunately, I wasn't trying to use a public/private key.不幸的是,我并没有尝试使用公钥/私钥。 I was only intending to log in with a username/password.我只想用用户名/密码登录。 I needed a way for it to stop trying to read my private key.我需要一种方法让它停止尝试读取我的私钥。

The root cause was that the code was using a default location for my key, and attempting to read it (even though thats not what I wanted).根本原因是代码使用了我的密钥的默认位置,并试图读取它(即使那不是我想要的)。

So the workaround was to override the default location by setting the following property:因此,解决方法是通过设置以下属性来覆盖默认位置:

 System.setProperty("vfs.sftp.sshdir", "/");

This bypassed the attempt to read the ssh key altogether, and successfully connected.这完全绕过了读取 ssh 密钥的尝试,并成功连接。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何修复使用 Apache Commons VFS 将文件上传到 SFTP 服务器时发生的错误 - How to fix error occurring while uploading file to SFTP server using Apache Commons VFS 使用 Apache Commons VFS 上传到远程 FTP 服务器 - Uploading to remote FTP server using Apache Commons VFS org.apache.commons.vfs2.FileSystemException:无法连接到 SFTP 服务器 - org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server SFTP上传下载使用Apache Commons VFS进行存在和移动 - SFTP Upload Download Exist and Move using Apache Commons VFS 使用Apache Commons VFS中断从SFTP中复制 - Copy from SFTP interrupted using Apache Commons VFS 使用Apache Commons Vfs2的SFTP文件传输 - SFTP file transfer using Apache Commons Vfs2 当使用 Apache Commons VFS 进行 SFTP 上传时,必须面对 org.apache.commons.vfs2.FileSystemException:找不到带有 URI 的文件 - When SFTP Upload using Apache Commons VFS have to face org.apache.commons.vfs2.FileSystemException: Could not find file with URI Android + JCraft JSch SFTP上传-“ org.apache.commons.vfs2.FileSystemException:无法在以下位置连接到SFTP服务器” - Android + JCraft JSch SFTP upload - “org.apache.commons.vfs2.FileSystemException: Could not connect to SFTP server at…” 使用 Apache Commons vfs2(spring boot) 的 SFTP 文件传输失败并出现 IOException:错误 - SFTP file transfer using Apache Commons vfs2(spring boot) fails with IOException: error 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
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM