问题
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();
}
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."
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.
回答1:
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,
try {
FileObject remote = fsManager.resolveFile("sftp://<USERNAME>:<PASSWORD>@<DOMAINNAME>.com/tmp1.zip",createDefaultOptions());
} catch(FileSystemException e) {
e.printStackTrace();
}
you may need to handle the FileSystemException accrodingly either through a try/catch or throwing the exception with method signature.
来源:https://stackoverflow.com/questions/57874812/uploading-to-remote-ftp-server-using-apache-commons-vfs