使用 Apache Commons VFS 上传到远程 FTP 服务器 [英] Uploading to remote FTP server using Apache Commons VFS

查看:43
本文介绍了使用 Apache Commons VFS 上传到远程 FTP 服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Apache VFS 将压缩文件上传到远程 FTP 服务器.如果相关,执行此操作的环境是 AWS Java 8 Lambda.这是我当前的实现,它通常遵循 此处 提供的示例:

I'm attempting to upload a zipped file to a remote FTP server using Apache VFS. The environment in which this is being executed in is an AWS Java 8 Lambda if it's relevant. This is my current implementation which generally follows the example provided here:

public static FileSystemOptions createDefaultOptions()
      throws FileSystemException {
    FileSystemOptions opts = new FileSystemOptions();

    SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
        opts, "no");

    SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);

    return opts;
}

public void uploadFile(File file) throws IOException {
    StandardFileSystemManager fsManager = new StandardFileSystemManager();
    fsManager.init();

    FileObject localFile = fsManager.resolveFile(file.getAbsolutePath());
    FileObject remote = fsManager.resolveFile(
        "sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip");

    remote.copyFrom(localFile, Selectors.SELECT_SELF);
    remote.close();
}

当我使用我确认存在于 Lambda 中的定义明确的文件调用此方法时,出现以下错误:

When I call this method with a well-defined file that I confirmed exists in the Lambda, I get an error of:

event_description="Could not find file with URI 
"sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip" 
because it is a relative path, and no base URI was provided."

我已经能够使用相同的凭据通过终端上的 FTP 连接到服务器,所以我认为这与这些无关.我看过的另一个可能相关的问题可以在这里找到这里,尽管我已经实施了它的建议,但仍然收到相同的无基础 URI"错误.

I have been able to connect to the server via FTP on the Terminal using the same credentials, so I don't think it's related to those. Another question which I've looked at which might be relevant can be found here here, although I've already implemented it's suggestion and am still getting the same "no base URI" error.

推荐答案

当你在 VFS 中通过 SFTP 解析远程文件时,你应该提供 FileSystemOptions 和 SftpFileSystemConfigs.所以,而不是,

You should provide the FileSystemOptions with SftpFileSystemConfigs when you are resolving the remote file through SFTP in VFS. So instead of,

FileObject remote = fsManager.resolveFile("sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip");

试试,

try {
    FileObject remote = fsManager.resolveFile("sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip",createDefaultOptions());
} catch(FileSystemException e) {
    e.printStackTrace();
}

您可能需要通过 try/catch 或使用方法签名抛出异常来逐步处理 FileSystemException.

you may need to handle the FileSystemException accrodingly either through a try/catch or throwing the exception with method signature.

这篇关于使用 Apache Commons VFS 上传到远程 FTP 服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆