I want to send some data from the database. Currently our SFTP is set up like so (using the apache commons vsf2 library).
try(StandardFileSystemManager manager = new StandardFileSystemManager())
File file = generateFileFromDatabase();
manager.init();
FileSystemOptions opts = new FileSystemOptions();
SftpFileSystemConfigBuilder.getInstance().setStrictHostKeyChecking(
opts, "no");
SftpFileSystemConfigBuilder.getInstance().setUserDirIsRoot(opts, true);
SftpFileSystemConfigBuilder.getInstance().setTimeout(opts, 10000);
// Create localfile 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);
} catch (Exception ex) {
ex.printStackTrace();
}
However, generateFileFromDatabase() works by invoking the new
keyword, as in
return new File();
I don't want this, because this saves a new file in the file system every time generateFileFromDatabase() is called. Is there a way to generate a File()
without saving it to the file directory?
Just use
OutputStream os = remoteFile.getContent().getOutputStream();
and then write In a loop to it, just like you would with a local file.
This might be a problem for slow reading/large transfers with long open connections. In that case you might need to pre-produce chunks and write them in multiple appends.