java - Apache VFS2 - 无法将文件上传到 SFTP 服务器

标签 java sftp apache-commons-vfs

我正在尝试使用 Apache VFS2 将文件上传到 SFTP 服务器。使用 WinSCP 等客户端时,SFTP 工作正常。我在互联网上找到了一些例子来使用java客户端,但我不断收到错误。使用的版本是2.3。代码:

public class SftpPersister
{
    private static final Logger logger          = Logger.getLogger( SftpPersister.class );

    String                      serverAddress   = "ftp.domain.com";
    String                      user            = "myuser";
    String                      password        = "mypass";
    String                      remoteDirectory = "outgoing/";
    String                      localDirectory  = "c:/users/user/";

   public static void main( String[] args )
   {
      new SftpPersister().upload( "ntuser.ini" );
   }

   public boolean upload( String fileName )
   {
      StandardFileSystemManager manager = new StandardFileSystemManager();

      try
      {

        //check if the file exists
        String filepath = localDirectory + fileName;
        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:///" + user + ":" + password + "@" + serverAddress + "/" + remoteDirectory + fileName;

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

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

        // Copy local file to sftp server
        remoteFile.copyFrom( localFile, Selectors.SELECT_SELF );

        logger.info( "File upload successful" );

    }
    catch ( Exception e )
    {
        logger.error( "failure", e );

        return false;
    }
    finally
    {
        manager.close();
    }

    return true;
}

}

执行remoteFile.copyFrom( localFile, Selectors.SELECT_SELF ) 行时抛出异常:

1 [main] ERROR com.company.middletier.storage.SftpPersister  - failure
org.apache.commons.vfs2.FileSystemException: Could not find file with URI "sftp:///myuser:***@ftp.domain.com/outgoing/ntuser.ini" because it is a relative path, and no base URI was provided.
at org.apache.commons.vfs2.FileSystemException.requireNonNull(FileSystemException.java:87)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:733)
at org.apache.commons.vfs2.impl.DefaultFileSystemManager.resolveFile(DefaultFileSystemManager.java:653)
at com.company.middletier.storage.SftpPersister.upload(SftpPersister.java:63)
at com.company.middletier.storage.SftpPersister.main(SftpPersister.java:31)

这是配置问题还是我遗漏的其他内容?

最佳答案

jsch 似乎是必需的运行时依赖项,需要添加到 POM 文件中。不确定文档中是否提到。添加后一切顺利。

关于java - Apache VFS2 - 无法将文件上传到 SFTP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56145381/

相关文章:

java - 2 在同一函数中返回触发?

Java mkdir + mkdirs 总是返回 false

c#-2.0 - 在2.0中实现SFTP

objective-c - 使用 iPad 无线传输文件

java - Apache commons sftp - 转到用户主目录上方

java - 有没有办法在android中删除/透明 snackbar 的背景颜色?

java - 确定Java程序的输入源

ssh - 将 Ed25519 转换为 RSA 指纹(或如何查找 SSH 指纹)

java - 让特殊文件夹在 Apache Commons VFS 中工作

java - 文件上传到 SFTP 失败(Apache VFS)