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

查看:392
本文介绍了使用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天全站免登陆