从数据库发送SFTP文件而不实际创建文件

huangapple go评论77阅读模式
英文:

Send SFTP file from database without actually creating a file

问题

我想从数据库中发送一些数据。目前我们的SFTP设置如下(使用apache commons vsf2库)。

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 local file object
    FileObject localFile = manager.resolveFile(file.getAbsolutePath());

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

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

} catch (Exception ex) {
    ex.printStackTrace();
}

然而,generateFileFromDatabase() 函数通过调用 new 关键字来工作,就像这样:

return new File();

我不想这样,因为每次调用 generateFileFromDatabase() 都会在文件系统中保存一个新文件。有没有一种方法可以生成 File(),而不将其保存到文件目录中?

英文:

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?

答案1

得分: 0

只需使用以下代码:

OutputStream os = remoteFile.getContent().getOutputStream();

然后像处理本地文件一样,在循环中写入数据。

对于读取缓慢/大文件且连接时间长的情况,可能需要预先生成数据块并以多次追加的方式写入。

英文:

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.

huangapple
  • 本文由 发表于 2020年8月4日 04:59:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/63236874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定